Investigating leaks in bug 463263, backout bug 453403.
[wine-gecko.git] / intl / unicharutil / src / nsHankakuToZenkaku.cpp
blobb0abe392ebae4702f3b45e8e52cbcfb63b9a165e
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1999
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
38 #include "nsITextTransform.h"
39 #include "pratom.h"
40 #include "nsUUDll.h"
41 #include "nsTextTransformFactory.h"
43 // Basic mapping from Hankaku to Zenkaku
44 // Nigori and Maru is take care out side this basic mapping
45 static const PRUnichar gBasicMapping[0x40] =
47 // 0xff60
48 0xff60,0x3002,0x300c,0x300d,0x3001,0x30fb,0x30f2,0x30a1,
49 // 0xff68
50 0x30a3,0x30a5,0x30a7,0x30a9,0x30e3,0x30e5,0x30e7,0x30c3,
51 // 0xff70
52 0x30fc,0x30a2,0x30a4,0x30a6,0x30a8,0x30aa,0x30ab,0x30ad,
53 // 0xff78
54 0x30af,0x30b1,0x30b3,0x30b5,0x30b7,0x30b9,0x30bb,0x30bd,
55 // 0xff80
56 0x30bf,0x30c1,0x30c4,0x30c6,0x30c8,0x30ca,0x30cb,0x30cc,
57 // 0xff88
58 0x30cd,0x30ce,0x30cf,0x30d2,0x30d5,0x30d8,0x30db,0x30de,
59 // 0xff90
60 0x30df,0x30e0,0x30e1,0x30e2,0x30e4,0x30e6,0x30e8,0x30e9,
61 // 0xff98
62 0x30ea,0x30eb,0x30ec,0x30ed,0x30ef,0x30f3,0x309b,0x309c
65 // Do we need to check for Nigori for the next unicode ?
66 #define NEED_TO_CHECK_NIGORI(u) (((0xff76<=(u))&&((u)<=0xff84))||((0xff8a<=(u))&&((u)<=0xff8e)))
68 // Do we need to check for Maru for the next unicode ?
69 #define NEED_TO_CHECK_MARU(u) ((0xff8a<=(u))&&((u)<=0xff8e))
71 // The unicode is in Katakana Hankaku block
72 #define IS_HANKAKU(u) (0xff60==((u) & 0xffe0)) || (0xff80==((u)&0xffe0))
73 #define IS_NIGORI(u) (0xff9e == (u))
74 #define IS_MARU(u) (0xff9f == (u))
75 #define NIGORI_MODIFIER 1
76 #define MARU_MODIFIER 2
78 // function prototype
79 void HankakuToZenkaku (
80 const PRUnichar* aSrc, PRInt32 aLen,
81 PRUnichar* aDest, PRInt32 aDestLen, PRInt32* oLen);
83 void HankakuToZenkaku (
84 const PRUnichar* aSrc, PRInt32 aLen,
85 PRUnichar* aDest, PRInt32 aDestLen, PRInt32* oLen)
87 // XXX aDestLen is never checked, assumed to be as long as aLen
88 NS_ASSERTION(aDestLen >= aLen, "aDest must be as long as aSrc");
90 PRInt32 i,j;
91 if ( aLen == 0) {
92 *oLen = 0;
93 return;
95 // loop from the first to the last char except the last one.
96 for(i = j = 0; i < (aLen-1); i++,j++,aSrc++, aDest++)
98 if(IS_HANKAKU(*aSrc)) {
99 // if it is in hankaku - do basic mapping first
100 *aDest = gBasicMapping[(*aSrc) - 0xff60];
102 // if is some char could be modifier, and the next char
103 // is a modifier, modify it and eat one byte
105 if(IS_NIGORI(*(aSrc+1)) && NEED_TO_CHECK_NIGORI(*aSrc))
107 *aDest += NIGORI_MODIFIER;
108 i++; aSrc++;
110 else if(IS_MARU(*(aSrc+1)) && NEED_TO_CHECK_MARU(*aSrc))
112 *aDest += MARU_MODIFIER;
113 i++; aSrc++;
116 else
118 // not in hankaku block, just copy
119 *aDest = *aSrc;
123 // handle the last character
124 if(IS_HANKAKU(*aSrc))
125 *aDest = gBasicMapping[(*aSrc) - 0xff60];
126 else
127 *aDest = *aSrc;
129 *oLen = j+1;
134 class nsHankakuToZenkaku : public nsITextTransform {
135 NS_DECL_ISUPPORTS
137 public:
139 nsHankakuToZenkaku() ;
140 virtual ~nsHankakuToZenkaku() ;
141 NS_IMETHOD Change( const PRUnichar* aText, PRInt32 aTextLength, nsString& aResult);
142 NS_IMETHOD Change( nsString& aText, nsString& aResult);
146 NS_IMPL_ISUPPORTS1(nsHankakuToZenkaku, nsITextTransform)
148 nsHankakuToZenkaku::nsHankakuToZenkaku()
151 nsHankakuToZenkaku::~nsHankakuToZenkaku()
155 NS_IMETHODIMP nsHankakuToZenkaku::Change( const PRUnichar* aText, PRInt32 aTextLength, nsString& aResult)
157 PRInt32 ol;
158 if (!EnsureStringLength(aResult, aTextLength))
159 return NS_ERROR_OUT_OF_MEMORY;
161 HankakuToZenkaku ( aText, aTextLength, aResult.BeginWriting(), aTextLength, &ol);
162 aResult.SetLength(ol);
164 return NS_OK;
167 NS_IMETHODIMP nsHankakuToZenkaku::Change( nsString& aText, nsString& aResult)
169 aResult = aText;
170 const PRUnichar* u = aResult.get();
171 PRUnichar* ou = (PRUnichar*) u;
172 PRInt32 l = aResult.Length();
173 PRInt32 ol;
175 HankakuToZenkaku ( u, l, ou, l, &ol);
176 aResult.SetLength(ol);
178 return NS_OK;
181 nsresult NS_NewHankakuToZenkaku(nsISupports** oResult)
183 if(!oResult)
184 return NS_ERROR_NULL_POINTER;
185 *oResult = new nsHankakuToZenkaku();
186 if(*oResult)
187 NS_ADDREF(*oResult);
188 return (*oResult) ? NS_OK : NS_ERROR_OUT_OF_MEMORY;