LMMS
Loading...
Searching...
No Matches
string_utils.h
Go to the documentation of this file.
1/*
2 Copyright 2011-2020 David Robillard <d@drobilla.net>
3
4 Permission to use, copy, modify, and/or distribute this software for any
5 purpose with or without fee is hereby granted, provided that the above
6 copyright notice and this permission notice appear in all copies.
7
8 THIS SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15*/
16
17#ifndef SERD_STRING_UTILS_H
18#define SERD_STRING_UTILS_H
19
20#include "serd/serd.h"
21
22#include <stdbool.h>
23#include <stddef.h>
24#include <stdint.h>
25
27static const uint8_t replacement_char[] = {0xEF, 0xBF, 0xBD};
28
30static inline bool
31in_range(const int c, const int min, const int max)
32{
33 return (c >= min && c <= max);
34}
35
37static inline bool
38is_alpha(const int c)
39{
40 return in_range(c, 'A', 'Z') || in_range(c, 'a', 'z');
41}
42
44static inline bool
45is_digit(const int c)
46{
47 return in_range(c, '0', '9');
48}
49
50/* RFC2234: HEXDIG ::= DIGIT / "A" / "B" / "C" / "D" / "E" / "F" */
51static inline bool
52is_hexdig(const int c)
53{
54 return is_digit(c) || in_range(c, 'A', 'F');
55}
56
57/* Turtle / JSON / C: XDIGIT ::= DIGIT / A-F / a-f */
58static inline bool
59is_xdigit(const int c)
60{
61 return is_hexdig(c) || in_range(c, 'a', 'f');
62}
63
64static inline bool
65is_space(const char c)
66{
67 switch (c) {
68 case ' ':
69 case '\f':
70 case '\n':
71 case '\r':
72 case '\t':
73 case '\v':
74 return true;
75 default:
76 return false;
77 }
78}
79
80static inline bool
81is_print(const int c)
82{
83 return c >= 0x20 && c <= 0x7E;
84}
85
86static inline bool
88{
89 return is_alpha(c) || is_digit(c) || c == '+' || c == '/' || c == '=';
90}
91
92static inline bool
94{
95 return is_alpha(path[0]) && (path[1] == ':' || path[1] == '|') &&
96 (path[2] == '/' || path[2] == '\\');
97}
98
99size_t
100serd_substrlen(const uint8_t* str,
101 size_t len,
102 size_t* n_bytes,
103 SerdNodeFlags* flags);
104
105static inline char
106serd_to_upper(const char c)
107{
108 return (char)((c >= 'a' && c <= 'z') ? c - 32 : c);
109}
110
111static inline int
112serd_strncasecmp(const char* s1, const char* s2, size_t n)
113{
114 for (; n > 0 && *s2; s1++, s2++, --n) {
115 if (serd_to_upper(*s1) != serd_to_upper(*s2)) {
116 return ((*(const uint8_t*)s1 < *(const uint8_t*)s2) ? -1 : +1);
117 }
118 }
119
120 return 0;
121}
122
123static inline uint32_t
125{
126 if ((c & 0x80) == 0) { // Starts with `0'
127 return 1;
128 }
129
130 if ((c & 0xE0) == 0xC0) { // Starts with `110'
131 return 2;
132 }
133
134 if ((c & 0xF0) == 0xE0) { // Starts with `1110'
135 return 3;
136 }
137
138 if ((c & 0xF8) == 0xF0) { // Starts with `11110'
139 return 4;
140 }
141
142 return 0;
143}
144
146static inline uint32_t
148{
149 uint32_t c = utf8[0] & ((1u << (8 - size)) - 1);
150 for (size_t i = 1; i < size; ++i) {
151 const uint8_t in = utf8[i] & 0x3F;
152 c = (c << 6) | in;
153 }
154 return c;
155}
156
158static inline uint32_t
159parse_utf8_char(const uint8_t* utf8, size_t* size)
160{
161 switch (*size = utf8_num_bytes(utf8[0])) {
162 case 1:
163 case 2:
164 case 3:
165 case 4:
166 return parse_counted_utf8_char(utf8, *size);
167 default:
168 *size = 0;
169 return 0;
170 }
171}
172
173#endif // SERD_STRING_UTILS_H
register unsigned i
Definition inflate.c:1575
uint32_t SerdNodeFlags
Bitwise OR of SerdNodeFlag values.
Definition serd.h:196
#define is_windows_path
float in
Definition lilv_test.c:1460
unsigned int uint32_t
Definition mid.cpp:100
unsigned char uint8_t
Definition mid.cpp:98
#define min(x, y)
Definition os.h:74
#define max(x, y)
Definition os.h:78
static uint32_t parse_utf8_char(const uint8_t *utf8, size_t *size)
Parse a UTF-8 character, set *size to the length, and return the code point.
Definition string_utils.h:159
static bool is_print(const int c)
Definition string_utils.h:81
static bool is_space(const char c)
Definition string_utils.h:65
static const uint8_t replacement_char[]
Definition string_utils.h:27
size_t serd_substrlen(const uint8_t *str, size_t len, size_t *n_bytes, SerdNodeFlags *flags)
Definition string.c:75
static int serd_strncasecmp(const char *s1, const char *s2, size_t n)
Definition string_utils.h:112
static bool in_range(const int c, const int min, const int max)
Definition string_utils.h:31
static uint32_t utf8_num_bytes(const uint8_t c)
Definition string_utils.h:124
static bool is_xdigit(const int c)
Definition string_utils.h:59
static uint32_t parse_counted_utf8_char(const uint8_t *utf8, size_t size)
Return the code point of a UTF-8 character with known length.
Definition string_utils.h:147
static bool is_digit(const int c)
Definition string_utils.h:45
static bool is_alpha(const int c)
Definition string_utils.h:38
static char serd_to_upper(const char c)
Definition string_utils.h:106
static bool is_hexdig(const int c)
Definition string_utils.h:52
static bool is_base64(const uint8_t c)
Definition string_utils.h:87
int n
Definition crypt.c:458
return c
Definition crypt.c:175
ulg size
Definition extract.c:2350