1 /* vifm 2 * Copyright (C) 2011 xaizek. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA 17 */ 18 19 #ifndef VIFM__UTILS__UTF8_H__ 20 #define VIFM__UTILS__UTF8_H__ 21 22 #include <stddef.h> /* size_t wchar_t */ 23 24 /* Abbreviations: 25 * - "n" -- "normal", excluding incomplete (broken) utf-8 sequences; 26 * - "sn" -- "screen number", screen width limit; 27 * - "w" -- "width", in bytes; 28 * - "o" -- "overhead"; 29 * - "sw" -- "screen width", i.e. number of character positions taken on the 30 * screen; 31 * - "so" -- "screen overhead". */ 32 33 /* Calculates real width of valid and complete utf-8 character in bytes. 34 * Returns the width. */ 35 size_t utf8_chrw(const char str[]); 36 37 /* Counts number of utf-8 characters excluding incomplete utf-8 characters. 38 * Returns the count. */ 39 size_t utf8_nstrlen(const char str[]); 40 41 /* Calculates count of bytes of whole str or of the first max_screen_width utf-8 42 * characters (so one character which takes several positions on the screen is 43 * counted as several positions). Returns the count. */ 44 size_t utf8_strsnlen(const char str[], size_t max_screen_width); 45 46 /* Same as utf8_strsnlen(), but ignores trailing incomplete utf-8 characters. */ 47 size_t utf8_nstrsnlen(const char str[], size_t max_screen_width); 48 49 /* Counts number of screen characters in a utf-8 encoded str. Returns the 50 * number. */ 51 size_t utf8_strsw(const char str[]); 52 53 /* Counts number of screen characters in a utf-8 encoded str expanding 54 * tabulation according to specified tab stops. tab_stops must be positive. 55 * Returns the number. */ 56 size_t utf8_strsw_with_tabs(const char str[], int tab_stops); 57 58 /* Gets screen width of the first character in the string. Returns the 59 * width or (size_t)-1 for unknown/broken characters. */ 60 size_t utf8_chrsw(const char str[]); 61 62 /* Calculates string overhead (string_bytes - string_chars). Returns the 63 * overhead. */ 64 size_t utf8_stro(const char str[]); 65 66 /* Calculates string screen overhead (string_bytes - string_screen_chars). 67 * Returns the overhead. */ 68 size_t utf8_strso(const char str[]); 69 70 /* Copies as many full utf-8 characters from source to destination as size of 71 * destination buffer permits. Returns number of actually copied bytes 72 * including terminating null character. */ 73 size_t utf8_strcpy(char dst[], const char src[], size_t dst_len); 74 75 #ifdef _WIN32 76 77 /* Converts utf-8 to utf-16 string. Returns newly allocated utf-8 string. */ 78 wchar_t * utf8_to_utf16(const char utf8[]); 79 80 /* Extracts first utf-8 character from the string converting it to wide 81 * character representation. Returns the wide character. */ 82 wchar_t utf8_first_char(const char utf8[]); 83 84 /* Calculates how many utf-16 chars are needed to store given utf-8 string. 85 * Returns the number. */ 86 size_t utf8_widen_len(const char utf8[]); 87 88 /* Converts utf-16 to utf-8 string. Returns newly allocated utf-16 string. */ 89 char * utf8_from_utf16(const wchar_t utf16[]); 90 91 #endif 92 93 #endif /* VIFM__UTILS__UTF8_H__ */ 94 95 /* vim: set tabstop=2 softtabstop=2 shiftwidth=2 noexpandtab cinoptions-=(0 : */ 96 /* vim: set cinoptions+=t0 filetype=c : */ 97