Skip to content

Instantly share code, notes, and snippets.

@zmike
Created November 26, 2014 23:55
Show Gist options
  • Select an option

  • Save zmike/ea956fc69318dfd07d35 to your computer and use it in GitHub Desktop.

Select an option

Save zmike/ea956fc69318dfd07d35 to your computer and use it in GitHub Desktop.
cef string usage
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#define CEF_STRING_TYPE_UTF8
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <assert.h>
#include "cef.h"
static void
verify_string(void *s)
{
cef_string_utf8_t *str1 = s;
assert(str1);
assert(!str1->str);
assert(!str1->length);
assert(!str1->dtor);
}
static void
compare_u8(cef_string_utf8_t *u8, const char *str, size_t len)
{
assert(!strcmp(u8->str, str));
assert(u8->length == strlen(u8->str));
assert(u8->length == len);
}
static void
init_u8(cef_string_utf8_t *u8, const char *str, size_t len)
{
assert(cef_string_utf8_set(str, len, u8, 1));
compare_u8(u8, str, len);
}
static void
free_u8(cef_string_utf8_t *u8)
{
cef_string_utf8_clear(u8);
assert(!u8->str);
assert(!u8->length);
cef_string_userfree_utf8_free(u8);
}
static void
test_utf8(void)
{
cef_string_utf8_t *str1;
str1 = cef_string_userfree_utf8_alloc();
verify_string(str1);
init_u8(str1, "test str", sizeof("test str" - 1));
free_u8(str1);
}
static void
compare_u16(cef_string_utf16_t *u16, char16 *str, size_t len)
{
int i;
for (i = 0; i < len; i++)
assert(u16->str[i] == str[i]);
assert(u16->length == len);
}
static void
init_u16(cef_string_utf16_t *u16, char16 *str, size_t len)
{
assert(cef_string_utf16_set(str, len, u16, 1));
compare_u16(u16, str, len);
}
static void
free_u16(cef_string_utf16_t *u16)
{
cef_string_utf16_clear(u16);
assert(!u16->str);
assert(!u16->length);
cef_string_userfree_utf16_free(u16);
}
static void
test_utf16(void)
{
cef_string_utf16_t *str1;
str1 = cef_string_userfree_utf16_alloc();
verify_string(str1);
init_u16(str1, (char16[]){'t','e','s','t',' ','s','t','r','\0'}, 8);
free_u16(str1);
}
static void
compare_wide(cef_string_wide_t *w, wchar_t *str, size_t len)
{
assert(!wcscmp(w->str, str));
assert(w->length == wcslen(w->str));
assert(w->length == len);
}
static void
init_wide(cef_string_wide_t *w, wchar_t *str, size_t len)
{
assert(cef_string_wide_set(str, len, w, 1));
compare_wide(w, str, len);
}
static void
free_wide(cef_string_wide_t *wide)
{
cef_string_wide_clear(wide);
assert(!wide->str);
assert(!wide->length);
cef_string_userfree_wide_free(wide);
}
static void
test_wide(void)
{
cef_string_wide_t *str1;
str1 = cef_string_userfree_wide_alloc();
verify_string(str1);
init_wide(str1, (wchar_t[]){'t','e','s','t',' ','s','t','r','\0'}, 8);
free_wide(str1);
}
static void
test_conv(void)
{
cef_string_wide_t *w;
cef_string_utf16_t *u16;
cef_string_utf8_t *u8;
w = cef_string_userfree_wide_alloc();
verify_string(w);
u16 = cef_string_userfree_utf16_alloc();
verify_string(u16);
u8 = cef_string_userfree_utf8_alloc();
verify_string(u8);
init_u8(u8, "test str", sizeof("test str") - 1);
assert(cef_string_utf8_to_utf16(u8->str, u8->length, u16));
compare_u16(u16, (char16[]){'t','e','s','t',' ','s','t','r','\0'}, 8);
// FIXME: need someone competent to write these
//assert(cef_string_utf8_to_wide(u8->str, u8->length, w));
//compare_wide(w, (wchar_t[]){'t','e','s','t',' ','s','t','r','\0'}, 8);
//assert(cef_string_utf16_to_wide(u16->str, u16->length, w));
//compare_wide(w, (wchar_t[]){'t','e','s','t',' ','s','t','r','\0'}, 8);
assert(cef_string_utf16_to_utf8(u16->str, u16->length, u8));
compare_u8(u8, "test str", 8);
//assert(cef_string_wide_to_utf8(w->str, w->length, u8));
//compare_u8(u8, "test str", 8);
free_u8(u8);
free_u16(u16);
free_wide(w);
}
int
main(int argc, char *argv[])
{
test_utf8();
test_utf16();
test_wide();
test_conv();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment