VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/string/utf-16-case.cpp@ 51770

Last change on this file since 51770 was 51770, checked in by vboxsync, 10 years ago

Merged in iprt++ dev branch.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
  • Property svn:mergeinfo set to (toggle deleted branches)
    /branches/VBox-3.0/src/VBox/Runtime/common/string/utf-16.cpp58652,​70973
    /branches/VBox-3.2/src/VBox/Runtime/common/string/utf-16.cpp66309,​66318
    /branches/VBox-4.0/src/VBox/Runtime/common/string/utf-16.cpp70873
    /branches/VBox-4.1/src/VBox/Runtime/common/string/utf-16.cpp74233,​78414,​78691,​81841,​82127,​85941,​85944-85947,​85949-85950,​85953,​86701,​86728,​87009
    /branches/VBox-4.2/src/VBox/Runtime/common/string/utf-16.cpp86229-86230,​86234,​86529,​91503-91504,​91506-91508,​91510,​91514-91515,​91521
    /branches/VBox-4.3/src/VBox/Runtime/common/string/utf-16.cpp91223
    /branches/VBox-4.3/trunk/src/VBox/Runtime/common/string/utf-16.cpp91223
    /branches/andy/draganddrop/src/VBox/Runtime/common/string/utf-16.cpp90781-91268
    /branches/andy/guestctrl20/src/VBox/Runtime/common/string/utf-16.cpp78916,​78930
    /branches/bird/hardenedwindows/src/VBox/Runtime/common/string/utf-16-case.cpp92961-94610
    /branches/dsen/gui/src/VBox/Runtime/common/string/utf-16.cpp79076-79078,​79089,​79109-79110,​79112-79113,​79127-79130,​79134,​79141,​79151,​79155,​79157-79159,​79193,​79197
    /branches/dsen/gui2/src/VBox/Runtime/common/string/utf-16.cpp79224,​79228,​79233,​79235,​79258,​79262-79263,​79273,​79341,​79345,​79354,​79357,​79387-79388,​79559-79569,​79572-79573,​79578,​79581-79582,​79590-79591,​79598-79599,​79602-79603,​79605-79606,​79632,​79635,​79637,​79644
    /branches/dsen/gui3/src/VBox/Runtime/common/string/utf-16.cpp79645-79692
File size: 5.6 KB
Line 
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
41RTDECL(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}
105RT_EXPORT_SYMBOL(RTUtf16ICmp);
106
107
108RTDECL(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}
143RT_EXPORT_SYMBOL(RTUtf16ToLower);
144
145
146RTDECL(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}
177RT_EXPORT_SYMBOL(RTUtf16ToUpper);
178
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette