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_build.h"
42 #include "include/internal/cef_export.h"
48 // CEF character type definitions. wchar_t is 2 bytes on Windows and 4 bytes on
49 // most other platforms.
52 typedef wchar_t char16;
54 typedef unsigned short char16; // NOLINT (runtime/int)
55 #ifndef WCHAR_T_IS_UTF32
56 #define WCHAR_T_IS_UTF32
57 #endif // WCHAR_T_IS_UTF32
61 // CEF string type definitions. Whomever allocates |str| is responsible for
62 // providing an appropriate |dtor| implementation that will free the string in
63 // the same memory space. When reusing an existing string structure make sure
64 // to call |dtor| for the old value before assigning new |str| and |dtor|
65 // values. Static strings will have a NULL |dtor| value. Using the below
66 // functions if you want this managed for you.
68 typedef struct _cef_string_wide_t {
71 void (*dtor)(wchar_t* str);
74 typedef struct _cef_string_utf8_t {
77 void (*dtor)(char* str);
80 typedef struct _cef_string_utf16_t {
83 void (*dtor)(char16* str);
88 // These functions set string values. If |copy| is true (1) the value will be
89 // copied instead of referenced. It is up to the user to properly manage
90 // the lifespan of references.
93 CEF_EXPORT int cef_string_wide_set(const wchar_t* src, size_t src_len,
94 cef_string_wide_t* output, int copy);
95 CEF_EXPORT int cef_string_utf8_set(const char* src, size_t src_len,
96 cef_string_utf8_t* output, int copy);
97 CEF_EXPORT int cef_string_utf16_set(const char16* src, size_t src_len,
98 cef_string_utf16_t* output, int copy);
102 // Convenience macros for copying values.
105 #define cef_string_wide_copy(src, src_len, output) \
106 cef_string_wide_set(src, src_len, output, true)
107 #define cef_string_utf8_copy(src, src_len, output) \
108 cef_string_utf8_set(src, src_len, output, true)
109 #define cef_string_utf16_copy(src, src_len, output) \
110 cef_string_utf16_set(src, src_len, output, true)
114 // These functions clear string values. The structure itself is not freed.
117 CEF_EXPORT void cef_string_wide_clear(cef_string_wide_t* str);
118 CEF_EXPORT void cef_string_utf8_clear(cef_string_utf8_t* str);
119 CEF_EXPORT void cef_string_utf16_clear(cef_string_utf16_t* str);
123 // These functions compare two string values with the same results as strcmp().
126 CEF_EXPORT int cef_string_wide_cmp(const cef_string_wide_t* str1,
127 const cef_string_wide_t* str2);
128 CEF_EXPORT int cef_string_utf8_cmp(const cef_string_utf8_t* str1,
129 const cef_string_utf8_t* str2);
130 CEF_EXPORT int cef_string_utf16_cmp(const cef_string_utf16_t* str1,
131 const cef_string_utf16_t* str2);
135 // These functions convert between UTF-8, -16, and -32 strings. They are
136 // potentially slow so unnecessary conversions should be avoided. The best
137 // possible result will always be written to |output| with the boolean return
138 // value indicating whether the conversion is 100% valid.
141 CEF_EXPORT int cef_string_wide_to_utf8(const wchar_t* src, size_t src_len,
142 cef_string_utf8_t* output);
143 CEF_EXPORT int cef_string_utf8_to_wide(const char* src, size_t src_len,
144 cef_string_wide_t* output);
146 CEF_EXPORT int cef_string_wide_to_utf16(const wchar_t* src, size_t src_len,
147 cef_string_utf16_t* output);
148 CEF_EXPORT int cef_string_utf16_to_wide(const char16* src, size_t src_len,
149 cef_string_wide_t* output);
151 CEF_EXPORT int cef_string_utf8_to_utf16(const char* src, size_t src_len,
152 cef_string_utf16_t* output);
153 CEF_EXPORT int cef_string_utf16_to_utf8(const char16* src, size_t src_len,
154 cef_string_utf8_t* output);
158 // These functions convert an ASCII string, typically a hardcoded constant, to a
159 // Wide/UTF16 string. Use instead of the UTF8 conversion routines if you know
160 // the string is ASCII.
163 CEF_EXPORT int cef_string_ascii_to_wide(const char* src, size_t src_len,
164 cef_string_wide_t* output);
165 CEF_EXPORT int cef_string_ascii_to_utf16(const char* src, size_t src_len,
166 cef_string_utf16_t* output);
171 // It is sometimes necessary for the system to allocate string structures with
172 // the expectation that the user will free them. The userfree types act as a
173 // hint that the user is responsible for freeing the structure.
176 typedef cef_string_wide_t* cef_string_userfree_wide_t;
177 typedef cef_string_utf8_t* cef_string_userfree_utf8_t;
178 typedef cef_string_utf16_t* cef_string_userfree_utf16_t;
182 // These functions allocate a new string structure. They must be freed by
183 // calling the associated free function.
186 CEF_EXPORT cef_string_userfree_wide_t cef_string_userfree_wide_alloc();
187 CEF_EXPORT cef_string_userfree_utf8_t cef_string_userfree_utf8_alloc();
188 CEF_EXPORT cef_string_userfree_utf16_t cef_string_userfree_utf16_alloc();
192 // These functions free the string structure allocated by the associated
193 // alloc function. Any string contents will first be cleared.
196 CEF_EXPORT void cef_string_userfree_wide_free(cef_string_userfree_wide_t str);
197 CEF_EXPORT void cef_string_userfree_utf8_free(cef_string_userfree_utf8_t str);
198 CEF_EXPORT void cef_string_userfree_utf16_free(cef_string_userfree_utf16_t str);
205 #endif // CEF_INCLUDE_INTERNAL_CEF_STRING_TYPES_H_