Skip to content

Instantly share code, notes, and snippets.

@Chase-san
Last active October 13, 2019 10:32
Show Gist options
  • Save Chase-san/a4d0755d7bc6780c295b to your computer and use it in GitHub Desktop.
Save Chase-san/a4d0755d7bc6780c295b to your computer and use it in GitHub Desktop.
A poor man's conversion from UCS-2/UTF-32 to UTF-8. Has limited support for UTF-16. As it uses wchar_t, this cannot easily be fixed.
/*
Copyright (c) 2014, Robert Maupin <chasesan>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "utf8.h"
#include <stdlib.h>
#include <string.h>
/* a poor man's iconv? */
size_t cptoutf8(char *dest, unsigned cp) {
if(cp < 0x80) {
dest[0] = cp;
return 1;
} else if (cp <= 0x7FF) {
dest[0] = (cp >> 6) + 0xC0;
dest[1] = (cp & 0x3F) + 0x80;
return 2;
} else if(cp <= 0xFFFF) {
dest[0] = (cp >> 12) + 0xE0;
dest[1] = ((cp >> 6) & 0x3F) + 0x80;
dest[2] = (cp & 0x3F) + 0x80;
return 3;
} else if(cp <= 0x10FFFF) {
dest[0] = (cp >> 18) + 0xF0;
dest[1] = ((cp >> 12) & 0x3F) + 0x80;
dest[2] = ((cp >> 6) & 0x3F) + 0x80;
dest[3] = (cp & 0x3F) + 0x80;
return 4;
}
return 0;
}
size_t wctoutf8(char *dest, wchar_t wc) {
/* This works for UCS-2, UTF-16 values <= 0xFFFF, and UTF-32 <= 0x10FFFF. */
return cptoutf8(dest, wc);
}
size_t wcsrtoutf8(char *dest, const wchar_t *src, size_t length) {
unsigned size = 0;
for(unsigned i = 0; i < length; ++i) {
unsigned ulen = wctoutf8(dest + size, src[i]);
if(src[i] == 0) {
break;
}
size += ulen;
}
return size;
}
char *wcstoutf8(const wchar_t *src) {
unsigned wlen = wcslen(src);
unsigned ulen = wlen * 4 + 1;
char *buf = malloc(ulen);
ulen = wcsrtoutf8(buf, src, wlen);
char *out = malloc(ulen + 1);
memcpy(out, buf, ulen);
out[ulen] = 0;
free(buf);
return out;
}
/*
Copyright (c) 2014, Robert Maupin <chasesan>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef __UTF8_H__
#define __UTF8_H__
#include <wchar.h>
size_t cptoutf8(char *dest, unsigned cp);
size_t wctoutf8(char *dest, wchar_t wc);
size_t wcsrtoutf8(char *dest, const wchar_t *src, size_t length);
char *wcstoutf8(const wchar_t *src);
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment