Pioneer
Loading...
Searching...
No Matches
TextSupport.h
Go to the documentation of this file.
1// Copyright © 2008-2023 Pioneer Developers. See AUTHORS.txt for details
2// Licensed under the terms of the GPL v3. See licenses/GPL-3.txt
3
4#ifndef _TEXT_TEXTSUPPORT_H
5#define _TEXT_TEXTSUPPORT_H
6
7#include <SDL_stdinc.h>
8#include <cassert>
9
10namespace Text {
11
12 // various text-related functions. a proper home needs to be found for them.
13
14 // convert one multibyte (utf8) char to a widechar (utf32/ucs4)
15 // chr: pointer to output storage
16 // src: multibyte string
17 // returns: number of bytes swallowed, or 0 if end of string
18 int utf8_decode_char(Uint32 *chr, const char *src);
19
20 // encode one Unicode code-point as UTF-8
21 // chr: the Unicode code-point
22 // buf: a character buffer, which must have space for at least 4 bytes
23 // (i.e., assigning to buf[3] must be a valid operation)
24 // returns: number of bytes in the encoded character
25 int utf8_encode_char(Uint32 chr, char buf[4]);
26
27 // this can tell you the length of a UTF-8 character, or more generally
28 // it tells you the number of bytes to move forward to get to the beginning
29 // of the next character
30 inline int utf8_next_char_offset(const char *str)
31 {
32 assert(str);
33 assert(*str);
34 const char *const start = str;
35 if (*str & 0x80) {
36 // technically, the first byte of a UTF-8 multi-byte sequence is enough
37 // to determine the length of the sequence, but a loop is simpler and
38 // more robust to incorrectly encoded text
39 do {
40 ++str;
41 } while ((*str & 0xC0) == 0x80);
42 return (str - start);
43 } else
44 return 1;
45 }
46
47 // this tells you the number of bytes to move backwards to get to the
48 // beginning of the previous character (or the current character if you start inside a multi-byte sequence)
49 // ('begin' indicates the start of the array and is used to avoid walking off the front)
50 inline int utf8_prev_char_offset(const char *str, const char *const begin)
51 {
52 assert(str);
53 assert(str > begin);
54 const char *const start = str;
55 --str;
56 if (*str & 0x80) {
57 while ((str > begin) && ((*str & 0xC0) == 0x80)) {
58 --str;
59 }
60 return (start - str);
61 } else
62 return 1;
63 }
64
65 // returns true if the char c is an ASCII letter, a digit
66 // or an underscore.
67 inline bool is_alphanumunderscore(char c)
68 {
69 return (c == '_' || (c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'));
70 }
71
72} // namespace Text
73
74#endif
Definition DistanceFieldFont.cpp:12
int utf8_encode_char(Uint32 chr, char buf[4])
Definition TextSupport.cpp:49
int utf8_decode_char(Uint32 *chr, const char *src)
Definition TextSupport.cpp:10
int utf8_prev_char_offset(const char *str, const char *const begin)
Definition TextSupport.h:50
bool is_alphanumunderscore(char c)
Definition TextSupport.h:67
int utf8_next_char_offset(const char *str)
Definition TextSupport.h:30