]> mj.ucw.cz Git - libucw.git/blob - ucw/string.h
1a52eb37de4036b3faaaa8eb72eb0ec4c9e1f0f8
[libucw.git] / ucw / string.h
1 /*
2  *      UCW Library -- String Routines
3  *
4  *      (c) 2006 Pavel Charvat <pchar@ucw.cz>
5  *      (c) 2007--2012 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_STRING_H
12 #define _UCW_STRING_H
13
14 #ifdef CONFIG_UCW_CLEAN_ABI
15 #define hex_to_mem ucw_hex_to_mem
16 #define mem_to_hex ucw_mem_to_hex
17 #define str_count_char ucw_str_count_char
18 #define str_format_flags ucw_str_format_flags
19 #define str_has_prefix ucw_str_has_prefix
20 #define str_has_suffix ucw_str_has_suffix
21 #define str_hier_prefix ucw_str_hier_prefix
22 #define str_hier_suffix ucw_str_hier_suffix
23 #define str_match_pattern ucw_str_match_pattern
24 #define str_match_pattern_nocase ucw_str_match_pattern_nocase
25 #define str_sepsplit ucw_str_sepsplit
26 #define str_unesc ucw_str_unesc
27 #define str_wordsplit ucw_str_wordsplit
28 #define strnlen ucw_strnlen
29 #endif
30
31 /* string.c */
32
33 #ifdef CONFIG_DARWIN
34 uns strnlen(const char *str, uns n);
35 #endif
36
37 /**
38  * Format a set of flag bits. When the i-th bit of @flags is 1,
39  * set the i-th character of @dest to @fmt[i], otherwise to '-'.
40  **/
41 char *str_format_flags(char *dest, const char *fmt, uns flags);
42
43 /** Counts occurrences of @chr in @str. **/
44 uns str_count_char(const char *str, uns chr);
45
46 /* str-esc.c */
47
48 /**
49  * Decode a string with backslash escape sequences as in C99 strings.
50  * It is safe to pass @dest equal to @src.
51  **/
52 char *str_unesc(char *dest, const char *src);
53
54 /* str-split.c */
55
56 /**
57  * Split @str to at most @max fields separated by @sep. Occurrences of the
58  * separator are rewritten to string terminators, @rec[i] is set to point
59  * to the i-th field. The total number of fields is returned.
60  *
61  * When there are more than @max fields in @str, the first @max fields
62  * are processed and -1 is returned.
63  **/
64 int str_sepsplit(char *str, uns sep, char **rec, uns max);
65
66 /**
67  * Split @str to words separated by white-space characters. The spaces
68  * are replaced by string terminators, @rec[i] is set to point to the
69  * i-th field. The total number of fields is returned.
70  *
71  * When there are more than @max fields in @str, the first @max fields
72  * are processed and -1 is returned.
73  *
74  * Fields surrounded by double quotes are also recognized. They can contain
75  * spaces, but no mechanism for escaping embedded quotes is defined.
76  **/
77 int str_wordsplit(char *str, char **rec, uns max);
78
79 /* str-(i)match.c: Matching of shell patterns */
80
81 /**
82  * Test whether the string @str matches the shell-like pattern @patt. Only
83  * "*" and "?" meta-characters are supported.
84  **/
85 int str_match_pattern(const char *patt, const char *str);
86
87 /** A case-insensitive version of @str_match_pattern(). **/
88 int str_match_pattern_nocase(const char *patt, const char *str);
89
90 /* str-hex.c */
91
92 /**
93  * Create a hexdump of a memory block of @bytes bytes starting at @src.
94  * The @flags contain an optional separator of bytes (0 if bytes should
95  * not be separated), possibly OR-ed with `MEM_TO_HEX_UPCASE` when upper-case
96  * characters should be used.
97  **/
98 void mem_to_hex(char *dest, const byte *src, uns bytes, uns flags);
99
100 /**
101  * An inverse function to @mem_to_hex(). Takes a hexdump of at most @max_bytes
102  * bytes and stores the bytes to a buffer starting at @dest. Returns a pointer
103  * at the first character after the dump.
104  **/
105 const char *hex_to_mem(byte *dest, const char *src, uns max_bytes, uns flags);
106
107 // Bottom 8 bits of flags are an optional separator of bytes, the rest is:
108 #define MEM_TO_HEX_UPCASE 0x100
109
110 /* str-fix.c */
111
112 int str_has_prefix(const char *str, const char *prefix); /** Tests if @str starts with @prefix. **/
113 int str_has_suffix(const char *str, const char *suffix); /** Tests if @str ends with @suffix. **/
114
115 /**
116  * Let @str and @prefix be hierarchical names with components separated by
117  * a character @sep. Returns true if @str starts with @prefix, respecting
118  * component boundaries.
119  *
120  * For example, when @sep is '/' and @str is "/usr/local/bin", then:
121  *
122  * - "/usr/local" is a prefix
123  * - "/usr/local/" is a prefix, too
124  * - "/usr/loc" is not,
125  * - "/usr/local/bin" is a prefix,
126  * - "/usr/local/bin/" is not,
127  * - "/" is a prefix,
128  * - "" is a prefix.
129  **/
130 int str_hier_prefix(const char *str, const char *prefix, uns sep);
131 int str_hier_suffix(const char *str, const char *suffix, uns sep); /** Like @str_hier_prefix(), but for suffixes. **/
132
133 #endif