1 | /* $Id: utf-16-case.cpp 60481 2016-04-13 20:14:41Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - UTF-16, Case Sensitivity.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2015 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * The contents of this file may alternatively be used under the terms
|
---|
18 | * of the Common Development and Distribution License Version 1.0
|
---|
19 | * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
|
---|
20 | * VirtualBox OSE distribution, in which case the provisions of the
|
---|
21 | * CDDL are applicable instead of those of the GPL.
|
---|
22 | *
|
---|
23 | * You may elect to license modified versions of this file under the
|
---|
24 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
25 | */
|
---|
26 |
|
---|
27 |
|
---|
28 | /*********************************************************************************************************************************
|
---|
29 | * Header Files *
|
---|
30 | *********************************************************************************************************************************/
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include "internal/iprt.h"
|
---|
33 |
|
---|
34 | #include <iprt/uni.h>
|
---|
35 | #include <iprt/alloc.h>
|
---|
36 | #include <iprt/assert.h>
|
---|
37 | #include <iprt/err.h>
|
---|
38 | #include "internal/string.h"
|
---|
39 |
|
---|
40 |
|
---|
41 | RTDECL(int) RTUtf16ICmp(register PCRTUTF16 pwsz1, register PCRTUTF16 pwsz2)
|
---|
42 | {
|
---|
43 | if (pwsz1 == pwsz2)
|
---|
44 | return 0;
|
---|
45 | if (!pwsz1)
|
---|
46 | return -1;
|
---|
47 | if (!pwsz2)
|
---|
48 | return 1;
|
---|
49 |
|
---|
50 | PCRTUTF16 pwsz1Start = pwsz1; /* keep it around in case we have to backtrack on a surrogate pair */
|
---|
51 | for (;;)
|
---|
52 | {
|
---|
53 | register RTUTF16 wc1 = *pwsz1;
|
---|
54 | register RTUTF16 wc2 = *pwsz2;
|
---|
55 | register int iDiff = wc1 - wc2;
|
---|
56 | if (iDiff)
|
---|
57 | {
|
---|
58 | /* unless they are *both* surrogate pairs, there is no chance they'll be identical. */
|
---|
59 | if ( wc1 < 0xd800
|
---|
60 | || wc2 < 0xd800
|
---|
61 | || wc1 > 0xdfff
|
---|
62 | || wc2 > 0xdfff)
|
---|
63 | {
|
---|
64 | /* simple UCS-2 char */
|
---|
65 | iDiff = RTUniCpToUpper(wc1) - RTUniCpToUpper(wc2);
|
---|
66 | if (iDiff)
|
---|
67 | iDiff = RTUniCpToLower(wc1) - RTUniCpToLower(wc2);
|
---|
68 | }
|
---|
69 | else
|
---|
70 | {
|
---|
71 | /* a damned pair */
|
---|
72 | RTUNICP uc1;
|
---|
73 | RTUNICP uc2;
|
---|
74 | if (wc1 >= 0xdc00)
|
---|
75 | {
|
---|
76 | if (pwsz1Start == pwsz1)
|
---|
77 | return iDiff;
|
---|
78 | uc1 = pwsz1[-1];
|
---|
79 | if (uc1 < 0xd800 || uc1 >= 0xdc00)
|
---|
80 | return iDiff;
|
---|
81 | uc1 = 0x10000 + (((uc1 & 0x3ff) << 10) | (wc1 & 0x3ff));
|
---|
82 | uc2 = 0x10000 + (((pwsz2[-1] & 0x3ff) << 10) | (wc2 & 0x3ff));
|
---|
83 | }
|
---|
84 | else
|
---|
85 | {
|
---|
86 | uc1 = *++pwsz1;
|
---|
87 | if (uc1 < 0xdc00 || uc1 >= 0xe000)
|
---|
88 | return iDiff;
|
---|
89 | uc1 = 0x10000 + (((wc1 & 0x3ff) << 10) | (uc1 & 0x3ff));
|
---|
90 | uc2 = 0x10000 + (((wc2 & 0x3ff) << 10) | (*++pwsz2 & 0x3ff));
|
---|
91 | }
|
---|
92 | iDiff = RTUniCpToUpper(uc1) - RTUniCpToUpper(uc2);
|
---|
93 | if (iDiff)
|
---|
94 | iDiff = RTUniCpToLower(uc1) - RTUniCpToLower(uc2); /* serious paranoia! */
|
---|
95 | }
|
---|
96 | if (iDiff)
|
---|
97 | return iDiff;
|
---|
98 | }
|
---|
99 | if (!wc1)
|
---|
100 | return 0;
|
---|
101 | pwsz1++;
|
---|
102 | pwsz2++;
|
---|
103 | }
|
---|
104 | }
|
---|
105 | RT_EXPORT_SYMBOL(RTUtf16ICmp);
|
---|
106 |
|
---|
107 |
|
---|
108 | RTDECL(int) RTUtf16ICmpUtf8(PCRTUTF16 pwsz1, const char *psz2)
|
---|
109 | {
|
---|
110 | /*
|
---|
111 | * NULL and empty strings are all the same.
|
---|
112 | */
|
---|
113 | if (!pwsz1)
|
---|
114 | return !psz2 || !*psz2 ? 0 : -1;
|
---|
115 | if (!psz2)
|
---|
116 | return !*pwsz1 ? 0 : 1;
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * Compare with a UTF-8 string by enumerating them char by char.
|
---|
120 | */
|
---|
121 | for (;;)
|
---|
122 | {
|
---|
123 | RTUNICP uc1;
|
---|
124 | int rc = RTUtf16GetCpEx(&pwsz1, &uc1);
|
---|
125 | AssertRCReturn(rc, 1);
|
---|
126 |
|
---|
127 | RTUNICP uc2;
|
---|
128 | rc = RTStrGetCpEx(&psz2, &uc2);
|
---|
129 | AssertRCReturn(rc, -1);
|
---|
130 | if (uc1 == uc2)
|
---|
131 | {
|
---|
132 | if (uc1)
|
---|
133 | continue;
|
---|
134 | return 0;
|
---|
135 | }
|
---|
136 |
|
---|
137 | if (RTUniCpToUpper(uc1) == RTUniCpToUpper(uc2))
|
---|
138 | continue;
|
---|
139 | if (RTUniCpToLower(uc1) == RTUniCpToLower(uc2))
|
---|
140 | continue;
|
---|
141 | return uc1 < uc2 ? -1 : 1;
|
---|
142 | }
|
---|
143 | }
|
---|
144 | RT_EXPORT_SYMBOL(RTUtf16CmpIUtf8);
|
---|
145 |
|
---|
146 |
|
---|
147 | RTDECL(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz)
|
---|
148 | {
|
---|
149 | PRTUTF16 pwc = pwsz;
|
---|
150 | for (;;)
|
---|
151 | {
|
---|
152 | RTUTF16 wc = *pwc;
|
---|
153 | if (!wc)
|
---|
154 | break;
|
---|
155 | if (wc < 0xd800 || wc >= 0xdc00)
|
---|
156 | {
|
---|
157 | RTUNICP ucFolded = RTUniCpToLower(wc);
|
---|
158 | if (ucFolded < 0x10000)
|
---|
159 | *pwc++ = RTUniCpToLower(wc);
|
---|
160 | }
|
---|
161 | else
|
---|
162 | {
|
---|
163 | /* surrogate */
|
---|
164 | RTUTF16 wc2 = pwc[1];
|
---|
165 | if (wc2 >= 0xdc00 && wc2 <= 0xdfff)
|
---|
166 | {
|
---|
167 | RTUNICP uc = 0x10000 + (((wc & 0x3ff) << 10) | (wc2 & 0x3ff));
|
---|
168 | RTUNICP ucFolded = RTUniCpToLower(uc);
|
---|
169 | if (uc != ucFolded && ucFolded >= 0x10000) /* we don't support shrinking the string */
|
---|
170 | {
|
---|
171 | uc -= 0x10000;
|
---|
172 | *pwc++ = 0xd800 | (uc >> 10);
|
---|
173 | *pwc++ = 0xdc00 | (uc & 0x3ff);
|
---|
174 | }
|
---|
175 | }
|
---|
176 | else /* invalid encoding. */
|
---|
177 | pwc++;
|
---|
178 | }
|
---|
179 | }
|
---|
180 | return pwsz;
|
---|
181 | }
|
---|
182 | RT_EXPORT_SYMBOL(RTUtf16ToLower);
|
---|
183 |
|
---|
184 |
|
---|
185 | RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz)
|
---|
186 | {
|
---|
187 | PRTUTF16 pwc = pwsz;
|
---|
188 | for (;;)
|
---|
189 | {
|
---|
190 | RTUTF16 wc = *pwc;
|
---|
191 | if (!wc)
|
---|
192 | break;
|
---|
193 | if (wc < 0xd800 || wc >= 0xdc00)
|
---|
194 | *pwc++ = RTUniCpToUpper(wc);
|
---|
195 | else
|
---|
196 | {
|
---|
197 | /* surrogate */
|
---|
198 | RTUTF16 wc2 = pwc[1];
|
---|
199 | if (wc2 >= 0xdc00 && wc2 <= 0xdfff)
|
---|
200 | {
|
---|
201 | RTUNICP uc = 0x10000 + (((wc & 0x3ff) << 10) | (wc2 & 0x3ff));
|
---|
202 | RTUNICP ucFolded = RTUniCpToUpper(uc);
|
---|
203 | if (uc != ucFolded && ucFolded >= 0x10000) /* we don't support shrinking the string */
|
---|
204 | {
|
---|
205 | uc -= 0x10000;
|
---|
206 | *pwc++ = 0xd800 | (uc >> 10);
|
---|
207 | *pwc++ = 0xdc00 | (uc & 0x3ff);
|
---|
208 | }
|
---|
209 | }
|
---|
210 | else /* invalid encoding. */
|
---|
211 | pwc++;
|
---|
212 | }
|
---|
213 | }
|
---|
214 | return pwsz;
|
---|
215 | }
|
---|
216 | RT_EXPORT_SYMBOL(RTUtf16ToUpper);
|
---|
217 |
|
---|