1 | /* $Id: utf-16-case.cpp 51770 2014-07-01 18:14:02Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - UTF-16, Case Sensitivity.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2014 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(PRTUTF16) RTUtf16ToLower(PRTUTF16 pwsz)
|
---|
109 | {
|
---|
110 | PRTUTF16 pwc = pwsz;
|
---|
111 | for (;;)
|
---|
112 | {
|
---|
113 | RTUTF16 wc = *pwc;
|
---|
114 | if (!wc)
|
---|
115 | break;
|
---|
116 | if (wc < 0xd800 || wc >= 0xdc00)
|
---|
117 | {
|
---|
118 | RTUNICP ucFolded = RTUniCpToLower(wc);
|
---|
119 | if (ucFolded < 0x10000)
|
---|
120 | *pwc++ = RTUniCpToLower(wc);
|
---|
121 | }
|
---|
122 | else
|
---|
123 | {
|
---|
124 | /* surrogate */
|
---|
125 | RTUTF16 wc2 = pwc[1];
|
---|
126 | if (wc2 >= 0xdc00 && wc2 <= 0xdfff)
|
---|
127 | {
|
---|
128 | RTUNICP uc = 0x10000 + (((wc & 0x3ff) << 10) | (wc2 & 0x3ff));
|
---|
129 | RTUNICP ucFolded = RTUniCpToLower(uc);
|
---|
130 | if (uc != ucFolded && ucFolded >= 0x10000) /* we don't support shrinking the string */
|
---|
131 | {
|
---|
132 | uc -= 0x10000;
|
---|
133 | *pwc++ = 0xd800 | (uc >> 10);
|
---|
134 | *pwc++ = 0xdc00 | (uc & 0x3ff);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | else /* invalid encoding. */
|
---|
138 | pwc++;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | return pwsz;
|
---|
142 | }
|
---|
143 | RT_EXPORT_SYMBOL(RTUtf16ToLower);
|
---|
144 |
|
---|
145 |
|
---|
146 | RTDECL(PRTUTF16) RTUtf16ToUpper(PRTUTF16 pwsz)
|
---|
147 | {
|
---|
148 | PRTUTF16 pwc = pwsz;
|
---|
149 | for (;;)
|
---|
150 | {
|
---|
151 | RTUTF16 wc = *pwc;
|
---|
152 | if (!wc)
|
---|
153 | break;
|
---|
154 | if (wc < 0xd800 || wc >= 0xdc00)
|
---|
155 | *pwc++ = RTUniCpToUpper(wc);
|
---|
156 | else
|
---|
157 | {
|
---|
158 | /* surrogate */
|
---|
159 | RTUTF16 wc2 = pwc[1];
|
---|
160 | if (wc2 >= 0xdc00 && wc2 <= 0xdfff)
|
---|
161 | {
|
---|
162 | RTUNICP uc = 0x10000 + (((wc & 0x3ff) << 10) | (wc2 & 0x3ff));
|
---|
163 | RTUNICP ucFolded = RTUniCpToUpper(uc);
|
---|
164 | if (uc != ucFolded && ucFolded >= 0x10000) /* we don't support shrinking the string */
|
---|
165 | {
|
---|
166 | uc -= 0x10000;
|
---|
167 | *pwc++ = 0xd800 | (uc >> 10);
|
---|
168 | *pwc++ = 0xdc00 | (uc & 0x3ff);
|
---|
169 | }
|
---|
170 | }
|
---|
171 | else /* invalid encoding. */
|
---|
172 | pwc++;
|
---|
173 | }
|
---|
174 | }
|
---|
175 | return pwsz;
|
---|
176 | }
|
---|
177 | RT_EXPORT_SYMBOL(RTUtf16ToUpper);
|
---|
178 |
|
---|