1 #ifndef TEXT_H
2 #define TEXT_H
3 
4 #include <stdbool.h>
5 #include <stdint.h>
6 
7 /** convert number of bytes to human readable string
8  *  returns number of characters written
9  *  notes: dest MUST be at least size characters large
10  */
11 int sprint_humanread_bytes(char *dest, unsigned int size, uint64_t bytes);
12 
13 /** length of a utf-8 character
14  *  returns the size of the character in bytes
15  *  returns -1 if the size of the character is greater than len or if the character is invalid
16  */
17 uint8_t utf8_len(const char *data);
18 /* read the character into ch */
19 uint8_t utf8_len_read(const char *data, uint32_t *ch);
20 /* backwards length */
21 uint8_t utf8_unlen(char *data);
22 
23 /* remove invalid characters from utf8 string
24  * returns the new length after invalid characters have been removed
25  */
26 int utf8_validate(const uint8_t *data, int len);
27 
28 uint8_t unicode_to_utf8_len(uint32_t ch);
29 void unicode_to_utf8(uint32_t ch, char *dst);
30 
31 /* compare first n bytes of s1 and s2, ignoring the case of alpha chars
32  *  match: returns 0
33  *  no match: returns 1
34  *  notes: n must be <= length of s1 and <= length of s2
35  */
36 bool memcmp_case(const char *s1, const char *s2, uint32_t n);
37 
38 /* replace html entities (<,>,&) with html
39  */
40 char *tohtml(const char *str, uint16_t len);
41 
42 void to_hex(char *out, uint8_t *in, int size);
43 
44 /* returns non-zero if substring is found */
45 bool strstr_case(const char *a, const char *b);
46 
47 /**
48  * @brief Shrink UTF-8 string down to provided length
49  * without splitting last UTF-8 multi-bytes character.
50  *
51  * @param string UTF-8 string to shrink.
52  * @param string_length Length of UTF-8 string.
53  * @param shrink_length Desirable length of shrunk string.
54  * @return shrunk length.
55  */
56 uint16_t safe_shrink(const char *string, uint16_t string_length, uint16_t shrink_length);
57 
58 #endif
59