xref: /aosp_15_r20/external/icu/libicu/cts_headers/ustr_imp.h (revision 0e209d3975ff4a8c132096b14b0e9364a753506e)
1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others.
2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html
3*0e209d39SAndroid Build Coastguard Worker /*
4*0e209d39SAndroid Build Coastguard Worker **********************************************************************
5*0e209d39SAndroid Build Coastguard Worker *   Copyright (C) 1999-2015, International Business Machines
6*0e209d39SAndroid Build Coastguard Worker *   Corporation and others.  All Rights Reserved.
7*0e209d39SAndroid Build Coastguard Worker **********************************************************************
8*0e209d39SAndroid Build Coastguard Worker *   file name:  ustr_imp.h
9*0e209d39SAndroid Build Coastguard Worker *   encoding:   UTF-8
10*0e209d39SAndroid Build Coastguard Worker *   tab size:   8 (not used)
11*0e209d39SAndroid Build Coastguard Worker *   indentation:4
12*0e209d39SAndroid Build Coastguard Worker *
13*0e209d39SAndroid Build Coastguard Worker *   created on: 2001jan30
14*0e209d39SAndroid Build Coastguard Worker *   created by: Markus W. Scherer
15*0e209d39SAndroid Build Coastguard Worker */
16*0e209d39SAndroid Build Coastguard Worker 
17*0e209d39SAndroid Build Coastguard Worker #ifndef __USTR_IMP_H__
18*0e209d39SAndroid Build Coastguard Worker #define __USTR_IMP_H__
19*0e209d39SAndroid Build Coastguard Worker 
20*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h"
21*0e209d39SAndroid Build Coastguard Worker #include "unicode/utf8.h"
22*0e209d39SAndroid Build Coastguard Worker 
23*0e209d39SAndroid Build Coastguard Worker /**
24*0e209d39SAndroid Build Coastguard Worker  * Internal option for unorm_cmpEquivFold() for strncmp style.
25*0e209d39SAndroid Build Coastguard Worker  * If set, checks for both string length and terminating NUL.
26*0e209d39SAndroid Build Coastguard Worker  */
27*0e209d39SAndroid Build Coastguard Worker #define _STRNCMP_STYLE 0x1000
28*0e209d39SAndroid Build Coastguard Worker 
29*0e209d39SAndroid Build Coastguard Worker /**
30*0e209d39SAndroid Build Coastguard Worker  * Compare two strings in code point order or code unit order.
31*0e209d39SAndroid Build Coastguard Worker  * Works in strcmp style (both lengths -1),
32*0e209d39SAndroid Build Coastguard Worker  * strncmp style (lengths equal and >=0, flag true),
33*0e209d39SAndroid Build Coastguard Worker  * and memcmp/UnicodeString style (at least one length >=0).
34*0e209d39SAndroid Build Coastguard Worker  */
35*0e209d39SAndroid Build Coastguard Worker U_CFUNC int32_t U_EXPORT2
36*0e209d39SAndroid Build Coastguard Worker uprv_strCompare(const UChar *s1, int32_t length1,
37*0e209d39SAndroid Build Coastguard Worker                 const UChar *s2, int32_t length2,
38*0e209d39SAndroid Build Coastguard Worker                 UBool strncmpStyle, UBool codePointOrder);
39*0e209d39SAndroid Build Coastguard Worker 
40*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
41*0e209d39SAndroid Build Coastguard Worker ustr_hashUCharsN(const UChar *str, int32_t length);
42*0e209d39SAndroid Build Coastguard Worker 
43*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
44*0e209d39SAndroid Build Coastguard Worker ustr_hashCharsN(const char *str, int32_t length);
45*0e209d39SAndroid Build Coastguard Worker 
46*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
47*0e209d39SAndroid Build Coastguard Worker ustr_hashICharsN(const char *str, int32_t length);
48*0e209d39SAndroid Build Coastguard Worker 
49*0e209d39SAndroid Build Coastguard Worker /**
50*0e209d39SAndroid Build Coastguard Worker  * Convert an ASCII-range lowercase character to uppercase.
51*0e209d39SAndroid Build Coastguard Worker  *
52*0e209d39SAndroid Build Coastguard Worker  * @param c A UChar.
53*0e209d39SAndroid Build Coastguard Worker  * @return If UChar is a lowercase ASCII character, returns the uppercase version.
54*0e209d39SAndroid Build Coastguard Worker  *         Otherwise, returns the input character.
55*0e209d39SAndroid Build Coastguard Worker  */
56*0e209d39SAndroid Build Coastguard Worker U_CAPI UChar U_EXPORT2
57*0e209d39SAndroid Build Coastguard Worker u_asciiToUpper(UChar c);
58*0e209d39SAndroid Build Coastguard Worker 
59*0e209d39SAndroid Build Coastguard Worker // TODO: Add u_asciiToLower if/when there is a need for it.
60*0e209d39SAndroid Build Coastguard Worker 
61*0e209d39SAndroid Build Coastguard Worker /**
62*0e209d39SAndroid Build Coastguard Worker  * NUL-terminate a UChar * string if possible.
63*0e209d39SAndroid Build Coastguard Worker  * If length  < destCapacity then NUL-terminate.
64*0e209d39SAndroid Build Coastguard Worker  * If length == destCapacity then do not terminate but set U_STRING_NOT_TERMINATED_WARNING.
65*0e209d39SAndroid Build Coastguard Worker  * If length  > destCapacity then do not terminate but set U_BUFFER_OVERFLOW_ERROR.
66*0e209d39SAndroid Build Coastguard Worker  *
67*0e209d39SAndroid Build Coastguard Worker  * @param dest Destination buffer, can be NULL if destCapacity==0.
68*0e209d39SAndroid Build Coastguard Worker  * @param destCapacity Number of UChars available at dest.
69*0e209d39SAndroid Build Coastguard Worker  * @param length Number of UChars that were (to be) written to dest.
70*0e209d39SAndroid Build Coastguard Worker  * @param pErrorCode ICU error code.
71*0e209d39SAndroid Build Coastguard Worker  * @return length
72*0e209d39SAndroid Build Coastguard Worker  */
73*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
74*0e209d39SAndroid Build Coastguard Worker u_terminateUChars(UChar *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode);
75*0e209d39SAndroid Build Coastguard Worker 
76*0e209d39SAndroid Build Coastguard Worker /**
77*0e209d39SAndroid Build Coastguard Worker  * NUL-terminate a char * string if possible.
78*0e209d39SAndroid Build Coastguard Worker  * Same as u_terminateUChars() but for a different string type.
79*0e209d39SAndroid Build Coastguard Worker  */
80*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
81*0e209d39SAndroid Build Coastguard Worker u_terminateChars(char *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode);
82*0e209d39SAndroid Build Coastguard Worker 
83*0e209d39SAndroid Build Coastguard Worker /**
84*0e209d39SAndroid Build Coastguard Worker  * NUL-terminate a UChar32 * string if possible.
85*0e209d39SAndroid Build Coastguard Worker  * Same as u_terminateUChars() but for a different string type.
86*0e209d39SAndroid Build Coastguard Worker  */
87*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
88*0e209d39SAndroid Build Coastguard Worker u_terminateUChar32s(UChar32 *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode);
89*0e209d39SAndroid Build Coastguard Worker 
90*0e209d39SAndroid Build Coastguard Worker /**
91*0e209d39SAndroid Build Coastguard Worker  * NUL-terminate a wchar_t * string if possible.
92*0e209d39SAndroid Build Coastguard Worker  * Same as u_terminateUChars() but for a different string type.
93*0e209d39SAndroid Build Coastguard Worker  */
94*0e209d39SAndroid Build Coastguard Worker U_CAPI int32_t U_EXPORT2
95*0e209d39SAndroid Build Coastguard Worker u_terminateWChars(wchar_t *dest, int32_t destCapacity, int32_t length, UErrorCode *pErrorCode);
96*0e209d39SAndroid Build Coastguard Worker 
97*0e209d39SAndroid Build Coastguard Worker /**
98*0e209d39SAndroid Build Coastguard Worker  * Counts the bytes of any whole valid sequence for a UTF-8 lead byte.
99*0e209d39SAndroid Build Coastguard Worker  * Returns 1 for ASCII 0..0x7f.
100*0e209d39SAndroid Build Coastguard Worker  * Returns 0 for 0x80..0xc1 as well as for 0xf5..0xff.
101*0e209d39SAndroid Build Coastguard Worker  * leadByte might be evaluated multiple times.
102*0e209d39SAndroid Build Coastguard Worker  *
103*0e209d39SAndroid Build Coastguard Worker  * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
104*0e209d39SAndroid Build Coastguard Worker  * @return 0..4
105*0e209d39SAndroid Build Coastguard Worker  */
106*0e209d39SAndroid Build Coastguard Worker #define U8_COUNT_BYTES(leadByte) \
107*0e209d39SAndroid Build Coastguard Worker     (U8_IS_SINGLE(leadByte) ? 1 : U8_COUNT_BYTES_NON_ASCII(leadByte))
108*0e209d39SAndroid Build Coastguard Worker 
109*0e209d39SAndroid Build Coastguard Worker /**
110*0e209d39SAndroid Build Coastguard Worker  * Counts the bytes of any whole valid sequence for a UTF-8 lead byte.
111*0e209d39SAndroid Build Coastguard Worker  * Returns 0 for 0x00..0xc1 as well as for 0xf5..0xff.
112*0e209d39SAndroid Build Coastguard Worker  * leadByte might be evaluated multiple times.
113*0e209d39SAndroid Build Coastguard Worker  *
114*0e209d39SAndroid Build Coastguard Worker  * @param leadByte The first byte of a UTF-8 sequence. Must be 0..0xff.
115*0e209d39SAndroid Build Coastguard Worker  * @return 0 or 2..4
116*0e209d39SAndroid Build Coastguard Worker  */
117*0e209d39SAndroid Build Coastguard Worker #define U8_COUNT_BYTES_NON_ASCII(leadByte) \
118*0e209d39SAndroid Build Coastguard Worker     (U8_IS_LEAD(leadByte) ? ((uint8_t)(leadByte)>=0xe0)+((uint8_t)(leadByte)>=0xf0)+2 : 0)
119*0e209d39SAndroid Build Coastguard Worker 
120*0e209d39SAndroid Build Coastguard Worker #ifdef __cplusplus
121*0e209d39SAndroid Build Coastguard Worker 
122*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN
123*0e209d39SAndroid Build Coastguard Worker 
124*0e209d39SAndroid Build Coastguard Worker class UTF8 {
125*0e209d39SAndroid Build Coastguard Worker public:
126*0e209d39SAndroid Build Coastguard Worker     UTF8() = delete;  // all static
127*0e209d39SAndroid Build Coastguard Worker 
128*0e209d39SAndroid Build Coastguard Worker     /**
129*0e209d39SAndroid Build Coastguard Worker      * Is t a valid UTF-8 trail byte?
130*0e209d39SAndroid Build Coastguard Worker      *
131*0e209d39SAndroid Build Coastguard Worker      * @param prev Must be the preceding lead byte if i==1 and length>=3;
132*0e209d39SAndroid Build Coastguard Worker      *             otherwise ignored.
133*0e209d39SAndroid Build Coastguard Worker      * @param t The i-th byte following the lead byte.
134*0e209d39SAndroid Build Coastguard Worker      * @param i The index (1..3) of byte t in the byte sequence. 0<i<length
135*0e209d39SAndroid Build Coastguard Worker      * @param length The length (2..4) of the byte sequence according to the lead byte.
136*0e209d39SAndroid Build Coastguard Worker      * @return true if t is a valid trail byte in this context.
137*0e209d39SAndroid Build Coastguard Worker      */
isValidTrail(int32_t prev,uint8_t t,int32_t i,int32_t length)138*0e209d39SAndroid Build Coastguard Worker     static inline UBool isValidTrail(int32_t prev, uint8_t t, int32_t i, int32_t length) {
139*0e209d39SAndroid Build Coastguard Worker         // The first trail byte after a 3- or 4-byte lead byte
140*0e209d39SAndroid Build Coastguard Worker         // needs to be validated together with its lead byte.
141*0e209d39SAndroid Build Coastguard Worker         if (length <= 2 || i > 1) {
142*0e209d39SAndroid Build Coastguard Worker             return U8_IS_TRAIL(t);
143*0e209d39SAndroid Build Coastguard Worker         } else if (length == 3) {
144*0e209d39SAndroid Build Coastguard Worker             return U8_IS_VALID_LEAD3_AND_T1(prev, t);
145*0e209d39SAndroid Build Coastguard Worker         } else {  // length == 4
146*0e209d39SAndroid Build Coastguard Worker             return U8_IS_VALID_LEAD4_AND_T1(prev, t);
147*0e209d39SAndroid Build Coastguard Worker         }
148*0e209d39SAndroid Build Coastguard Worker     }
149*0e209d39SAndroid Build Coastguard Worker };
150*0e209d39SAndroid Build Coastguard Worker 
151*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END
152*0e209d39SAndroid Build Coastguard Worker 
153*0e209d39SAndroid Build Coastguard Worker #endif  // __cplusplus
154*0e209d39SAndroid Build Coastguard Worker 
155*0e209d39SAndroid Build Coastguard Worker #endif
156