1 // Copyright (c) 2010 Marshall A. Greenblatt. All rights reserved.
3 // Redistribution and use in source and binary forms, with or without
4 // modification, are permitted provided that the following conditions are
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above
10 // copyright notice, this list of conditions and the following disclaimer
11 // in the documentation and/or other materials provided with the
13 // * Neither the name of Google Inc. nor the name Chromium Embedded
14 // Framework nor the names of its contributors may be used to endorse
15 // or promote products derived from this software without specific prior
16 // written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #ifndef CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_
31 #define CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_
34 // CEF provides functions for converting between UTF-8, -16 and -32 strings.
35 // CEF string types are safe for reading from multiple threads but not for
36 // modification. It is the user's responsibility to provide synchronization if
37 // modifying CEF strings from multiple threads.
41 #include "include/base/cef_basictypes.h"
42 #include "include/internal/cef_export.h"
48 // CEF string type definitions. Whomever allocates |str| is responsible for
49 // providing an appropriate |dtor| implementation that will free the string in
50 // the same memory space. When reusing an existing string structure make sure
51 // to call |dtor| for the old value before assigning new |str| and |dtor|
52 // values. Static strings will have a NULL |dtor| value. Using the below
53 // functions if you want this managed for you.
55 typedef struct _cef_string_wide_t {
58 void (*dtor)(wchar_t* str);
61 typedef struct _cef_string_utf8_t {
64 void (*dtor)(char* str);
67 typedef struct _cef_string_utf16_t {
70 void (*dtor)(char16* str);
75 // These functions set string values. If |copy| is true (1) the value will be
76 // copied instead of referenced. It is up to the user to properly manage
77 // the lifespan of references.
80 CEF_EXPORT int cef_string_wide_set(const wchar_t* src, size_t src_len,
81 cef_string_wide_t* output, int copy);
82 CEF_EXPORT int cef_string_utf8_set(const char* src, size_t src_len,
83 cef_string_utf8_t* output, int copy);
84 CEF_EXPORT int cef_string_utf16_set(const char16* src, size_t src_len,
85 cef_string_utf16_t* output, int copy);
89 // Convenience macros for copying values.
92 #define cef_string_wide_copy(src, src_len, output) \
93 cef_string_wide_set(src, src_len, output, true)
94 #define cef_string_utf8_copy(src, src_len, output) \
95 cef_string_utf8_set(src, src_len, output, true)
96 #define cef_string_utf16_copy(src, src_len, output) \
97 cef_string_utf16_set(src, src_len, output, true)
101 // These functions clear string values. The structure itself is not freed.
104 CEF_EXPORT void cef_string_wide_clear(cef_string_wide_t* str);
105 CEF_EXPORT void cef_string_utf8_clear(cef_string_utf8_t* str);
106 CEF_EXPORT void cef_string_utf16_clear(cef_string_utf16_t* str);
110 // These functions compare two string values with the same results as strcmp().
113 CEF_EXPORT int cef_string_wide_cmp(const cef_string_wide_t* str1,
114 const cef_string_wide_t* str2);
115 CEF_EXPORT int cef_string_utf8_cmp(const cef_string_utf8_t* str1,
116 const cef_string_utf8_t* str2);
117 CEF_EXPORT int cef_string_utf16_cmp(const cef_string_utf16_t* str1,
118 const cef_string_utf16_t* str2);
122 // These functions convert between UTF-8, -16, and -32 strings. They are
123 // potentially slow so unnecessary conversions should be avoided. The best
124 // possible result will always be written to |output| with the boolean return
125 // value indicating whether the conversion is 100% valid.
128 CEF_EXPORT int cef_string_wide_to_utf8(const wchar_t* src, size_t src_len,
129 cef_string_utf8_t* output);
130 CEF_EXPORT int cef_string_utf8_to_wide(const char* src, size_t src_len,
131 cef_string_wide_t* output);
133 CEF_EXPORT int cef_string_wide_to_utf16(const wchar_t* src, size_t src_len,
134 cef_string_utf16_t* output);
135 CEF_EXPORT int cef_string_utf16_to_wide(const char16* src, size_t src_len,
136 cef_string_wide_t* output);
138 CEF_EXPORT int cef_string_utf8_to_utf16(const char* src, size_t src_len,
139 cef_string_utf16_t* output);
140 CEF_EXPORT int cef_string_utf16_to_utf8(const char16* src, size_t src_len,
141 cef_string_utf8_t* output);
145 // These functions convert an ASCII string, typically a hardcoded constant, to a
146 // Wide/UTF16 string. Use instead of the UTF8 conversion routines if you know
147 // the string is ASCII.
150 CEF_EXPORT int cef_string_ascii_to_wide(const char* src, size_t src_len,
151 cef_string_wide_t* output);
152 CEF_EXPORT int cef_string_ascii_to_utf16(const char* src, size_t src_len,
153 cef_string_utf16_t* output);
158 // It is sometimes necessary for the system to allocate string structures with
159 // the expectation that the user will free them. The userfree types act as a
160 // hint that the user is responsible for freeing the structure.
163 typedef cef_string_wide_t* cef_string_userfree_wide_t;
164 typedef cef_string_utf8_t* cef_string_userfree_utf8_t;
165 typedef cef_string_utf16_t* cef_string_userfree_utf16_t;
169 // These functions allocate a new string structure. They must be freed by
170 // calling the associated free function.
173 CEF_EXPORT cef_string_userfree_wide_t cef_string_userfree_wide_alloc();
174 CEF_EXPORT cef_string_userfree_utf8_t cef_string_userfree_utf8_alloc();
175 CEF_EXPORT cef_string_userfree_utf16_t cef_string_userfree_utf16_alloc();
179 // These functions free the string structure allocated by the associated
180 // alloc function. Any string contents will first be cleared.
183 CEF_EXPORT void cef_string_userfree_wide_free(cef_string_userfree_wide_t str);
184 CEF_EXPORT void cef_string_userfree_utf8_free(cef_string_userfree_utf8_t str);
185 CEF_EXPORT void cef_string_userfree_utf16_free(cef_string_userfree_utf16_t str);
189 // These functions convert utf16 string case using the current ICU locale. This
190 // may change the length of the string in some cases.
193 CEF_EXPORT int cef_string_utf16_to_lower(const char16* src, size_t src_len,
194 cef_string_utf16_t* output);
195 CEF_EXPORT int cef_string_utf16_to_upper(const char16* src, size_t src_len,
196 cef_string_utf16_t* output);
203 #endif // CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_