1 1.1 christos /* 2 1.1 christos * Copyright (c) 2016 Joshua Rubin <joshua (at) rubixconsulting.com> 3 1.1 christos * 4 1.1 christos * Permission to use, copy, modify, and distribute this software for any 5 1.1 christos * purpose with or without fee is hereby granted, provided that the above 6 1.1 christos * copyright notice and this permission notice appear in all copies. 7 1.1 christos * 8 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 9 1.1 christos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 10 1.1 christos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 11 1.1 christos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 12 1.1 christos * WHATSOEVER RESULTING FROM LOSS OF MIND, USE, DATA OR PROFITS, WHETHER 13 1.1 christos * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING 14 1.1 christos * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 15 1.1 christos */ 16 1.1 christos 17 1.1 christos #include <sys/types.h> 18 1.1 christos 19 1.1 christos #include <utf8proc.h> 20 1.1 christos 21 1.1 christos #include "compat.h" 22 1.1 christos 23 1.1 christos int 24 1.1 christos utf8proc_wcwidth(wchar_t wc) 25 1.1 christos { 26 1.1 christos int cat; 27 1.1 christos 28 1.1 christos cat = utf8proc_category(wc); 29 1.1 christos if (cat == UTF8PROC_CATEGORY_CO) { 30 1.1 christos /* 31 1.1 christos * The private use category is where powerline and similar 32 1.1 christos * codepoints are stored, they have "ambiguous" width - use 1. 33 1.1 christos */ 34 1.1 christos return (1); 35 1.1 christos } 36 1.1 christos return (utf8proc_charwidth(wc)); 37 1.1 christos } 38 1.1 christos 39 1.1 christos int 40 1.1 christos utf8proc_mbtowc(wchar_t *pwc, const char *s, size_t n) 41 1.1 christos { 42 1.1 christos utf8proc_ssize_t slen; 43 1.1 christos 44 1.1 christos if (s == NULL) 45 1.1 christos return (0); 46 1.1 christos 47 1.1 christos /* 48 1.1 christos * *pwc == -1 indicates invalid codepoint 49 1.1 christos * slen < 0 indicates an error 50 1.1 christos */ 51 1.1.1.2 wiz slen = utf8proc_iterate(s, n, (utf8proc_int32_t*)pwc); 52 1.1 christos if (*pwc == (wchar_t)-1 || slen < 0) 53 1.1 christos return (-1); 54 1.1 christos return (slen); 55 1.1 christos } 56 1.1 christos 57 1.1 christos int 58 1.1 christos utf8proc_wctomb(char *s, wchar_t wc) 59 1.1 christos { 60 1.1 christos if (s == NULL) 61 1.1 christos return (0); 62 1.1 christos 63 1.1 christos if (!utf8proc_codepoint_valid(wc)) 64 1.1 christos return (-1); 65 1.1 christos return (utf8proc_encode_char(wc, s)); 66 1.1 christos } 67