backburn 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/LICENSE +21 -0
- data/Makefile +50 -0
- data/README.md +520 -0
- data/docs/cli-and-outputs.md +226 -0
- data/docs/technical-spec.md +656 -0
- data/exe/backburn +8 -0
- data/ext/backburn/extconf.rb +31 -0
- data/ext/backburn/prism/COMMIT +1 -0
- data/ext/backburn/prism/LICENSE.md +7 -0
- data/ext/backburn/prism/Makefile +213 -0
- data/ext/backburn/prism/include/prism/arena.h +45 -0
- data/ext/backburn/prism/include/prism/arena.hpp +42 -0
- data/ext/backburn/prism/include/prism/ast.h +10395 -0
- data/ext/backburn/prism/include/prism/ast.hpp +641 -0
- data/ext/backburn/prism/include/prism/buffer.h +60 -0
- data/ext/backburn/prism/include/prism/buffer.hpp +55 -0
- data/ext/backburn/prism/include/prism/comments.h +72 -0
- data/ext/backburn/prism/include/prism/compiler/accel.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/align.h +36 -0
- data/ext/backburn/prism/include/prism/compiler/assume.h +25 -0
- data/ext/backburn/prism/include/prism/compiler/branch.h +26 -0
- data/ext/backburn/prism/include/prism/compiler/bswap.h +46 -0
- data/ext/backburn/prism/include/prism/compiler/cplusplus.hpp +16 -0
- data/ext/backburn/prism/include/prism/compiler/endianness.h +34 -0
- data/ext/backburn/prism/include/prism/compiler/exceptions.hpp +17 -0
- data/ext/backburn/prism/include/prism/compiler/exported.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/fallthrough.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/filesystem.h +32 -0
- data/ext/backburn/prism/include/prism/compiler/flex_array.h +19 -0
- data/ext/backburn/prism/include/prism/compiler/force_inline.h +21 -0
- data/ext/backburn/prism/include/prism/compiler/format.h +27 -0
- data/ext/backburn/prism/include/prism/compiler/inline.h +17 -0
- data/ext/backburn/prism/include/prism/compiler/lifetimebound.hpp +25 -0
- data/ext/backburn/prism/include/prism/compiler/nodiscard.h +22 -0
- data/ext/backburn/prism/include/prism/compiler/nonnull.h +18 -0
- data/ext/backburn/prism/include/prism/compiler/noreturn.h +24 -0
- data/ext/backburn/prism/include/prism/compiler/stack.h +60 -0
- data/ext/backburn/prism/include/prism/compiler/unused.h +18 -0
- data/ext/backburn/prism/include/prism/constant_pool.h +112 -0
- data/ext/backburn/prism/include/prism/diagnostic.h +121 -0
- data/ext/backburn/prism/include/prism/errors_format.h +63 -0
- data/ext/backburn/prism/include/prism/excludes.h +29 -0
- data/ext/backburn/prism/include/prism/integer.h +79 -0
- data/ext/backburn/prism/include/prism/internal/allocator.h +68 -0
- data/ext/backburn/prism/include/prism/internal/allocator_debug.h +88 -0
- data/ext/backburn/prism/include/prism/internal/arena.h +223 -0
- data/ext/backburn/prism/include/prism/internal/assert.h +14 -0
- data/ext/backburn/prism/include/prism/internal/bignum.h +239 -0
- data/ext/backburn/prism/include/prism/internal/bit.h +66 -0
- data/ext/backburn/prism/include/prism/internal/buffer.h +104 -0
- data/ext/backburn/prism/include/prism/internal/char.h +145 -0
- data/ext/backburn/prism/include/prism/internal/comments.h +54 -0
- data/ext/backburn/prism/include/prism/internal/constant_pool.h +155 -0
- data/ext/backburn/prism/include/prism/internal/diagnostic.h +392 -0
- data/ext/backburn/prism/include/prism/internal/double.h +19 -0
- data/ext/backburn/prism/include/prism/internal/encoding.h +326 -0
- data/ext/backburn/prism/include/prism/internal/integer.h +66 -0
- data/ext/backburn/prism/include/prism/internal/isinf.h +16 -0
- data/ext/backburn/prism/include/prism/internal/line_offset_list.h +50 -0
- data/ext/backburn/prism/include/prism/internal/list.h +62 -0
- data/ext/backburn/prism/include/prism/internal/magic_comments.h +23 -0
- data/ext/backburn/prism/include/prism/internal/memchr.h +15 -0
- data/ext/backburn/prism/include/prism/internal/node.h +197 -0
- data/ext/backburn/prism/include/prism/internal/oom.h +16 -0
- data/ext/backburn/prism/include/prism/internal/options.h +248 -0
- data/ext/backburn/prism/include/prism/internal/parser.h +1170 -0
- data/ext/backburn/prism/include/prism/internal/regexp.h +70 -0
- data/ext/backburn/prism/include/prism/internal/serialize.h +39 -0
- data/ext/backburn/prism/include/prism/internal/source.h +72 -0
- data/ext/backburn/prism/include/prism/internal/static_literals.h +60 -0
- data/ext/backburn/prism/include/prism/internal/stringy.h +37 -0
- data/ext/backburn/prism/include/prism/internal/strncasecmp.h +18 -0
- data/ext/backburn/prism/include/prism/internal/strpbrk.h +61 -0
- data/ext/backburn/prism/include/prism/internal/tokens.h +11 -0
- data/ext/backburn/prism/include/prism/internal/xxh3.h +16 -0
- data/ext/backburn/prism/include/prism/json.h +40 -0
- data/ext/backburn/prism/include/prism/line_offset_list.h +69 -0
- data/ext/backburn/prism/include/prism/magic_comments.h +53 -0
- data/ext/backburn/prism/include/prism/node.h +139 -0
- data/ext/backburn/prism/include/prism/node.hpp +272 -0
- data/ext/backburn/prism/include/prism/node_type.hpp +38 -0
- data/ext/backburn/prism/include/prism/options.h +393 -0
- data/ext/backburn/prism/include/prism/options.hpp +43 -0
- data/ext/backburn/prism/include/prism/parser.h +433 -0
- data/ext/backburn/prism/include/prism/parser.hpp +274 -0
- data/ext/backburn/prism/include/prism/prettyprint.h +39 -0
- data/ext/backburn/prism/include/prism/serialize.h +123 -0
- data/ext/backburn/prism/include/prism/source.h +163 -0
- data/ext/backburn/prism/include/prism/stream.h +36 -0
- data/ext/backburn/prism/include/prism/string_query.h +97 -0
- data/ext/backburn/prism/include/prism/stringy.h +61 -0
- data/ext/backburn/prism/include/prism/stringy.hpp +31 -0
- data/ext/backburn/prism/include/prism/version.h +46 -0
- data/ext/backburn/prism/include/prism.h +123 -0
- data/ext/backburn/prism/include/prism.hpp +24 -0
- data/ext/backburn/prism/src/arena.c +200 -0
- data/ext/backburn/prism/src/bignum.c +399 -0
- data/ext/backburn/prism/src/buffer.c +405 -0
- data/ext/backburn/prism/src/char.c +289 -0
- data/ext/backburn/prism/src/comments.c +455 -0
- data/ext/backburn/prism/src/constant_pool.c +410 -0
- data/ext/backburn/prism/src/diagnostic.c +918 -0
- data/ext/backburn/prism/src/double.c +394 -0
- data/ext/backburn/prism/src/encoding.c +1701 -0
- data/ext/backburn/prism/src/errors_format.c +650 -0
- data/ext/backburn/prism/src/integer.c +675 -0
- data/ext/backburn/prism/src/json.c +5745 -0
- data/ext/backburn/prism/src/line_offset_list.c +99 -0
- data/ext/backburn/prism/src/list.c +24 -0
- data/ext/backburn/prism/src/memchr.c +37 -0
- data/ext/backburn/prism/src/node.c +4464 -0
- data/ext/backburn/prism/src/options.c +475 -0
- data/ext/backburn/prism/src/parser.c +371 -0
- data/ext/backburn/prism/src/prettyprint.c +9038 -0
- data/ext/backburn/prism/src/prism.c +23615 -0
- data/ext/backburn/prism/src/regexp.c +3833 -0
- data/ext/backburn/prism/src/serialize.c +2656 -0
- data/ext/backburn/prism/src/source.c +525 -0
- data/ext/backburn/prism/src/static_literals.c +562 -0
- data/ext/backburn/prism/src/string_query.c +195 -0
- data/ext/backburn/prism/src/stringy.c +77 -0
- data/ext/backburn/prism/src/strncasecmp.c +37 -0
- data/ext/backburn/prism/src/strpbrk.c +465 -0
- data/ext/backburn/prism/src/tokens.c +724 -0
- data/ext/backburn/prism/src/xxh3.c +292 -0
- data/lib/backburn/version.rb +7 -0
- data/presets/devise.roots +11 -0
- data/presets/fast-mcp.roots +5 -0
- data/presets/graphql.roots +6 -0
- data/presets/phlex.roots +7 -0
- data/presets/pundit.roots +3 -0
- data/presets/rails-framework.roots +10 -0
- data/presets/rails.roots +83 -0
- data/presets/sidekiq.roots +4 -0
- data/presets/thor.roots +3 -0
- data/rails.roots +99 -0
- data/scripts/embed-presets.sh +13 -0
- data/scripts/version-h.sh +7 -0
- data/src/analysis/analysis.c +95 -0
- data/src/analysis/analysis.h +24 -0
- data/src/analysis/batches.c +127 -0
- data/src/analysis/batches.h +17 -0
- data/src/analysis/graph.c +238 -0
- data/src/analysis/graph.h +34 -0
- data/src/analysis/mark.c +300 -0
- data/src/analysis/mark.h +21 -0
- data/src/analysis/roots.c +229 -0
- data/src/analysis/roots.h +29 -0
- data/src/cli/cli.c +237 -0
- data/src/cli/cli.h +9 -0
- data/src/cli/config.c +112 -0
- data/src/cli/config.h +14 -0
- data/src/cli/options.c +17 -0
- data/src/cli/options.h +35 -0
- data/src/commands/commands.h +11 -0
- data/src/commands/explain.c +255 -0
- data/src/commands/gemfile.c +126 -0
- data/src/commands/gemfile.h +27 -0
- data/src/commands/gems.c +408 -0
- data/src/commands/init.c +92 -0
- data/src/commands/roots.c +82 -0
- data/src/commands/scan.c +91 -0
- data/src/core/intern.c +94 -0
- data/src/core/intern.h +40 -0
- data/src/core/json.c +17 -0
- data/src/core/json.h +9 -0
- data/src/core/model.c +25 -0
- data/src/core/model.h +85 -0
- data/src/core/util.c +116 -0
- data/src/core/util.h +48 -0
- data/src/index/erb.c +40 -0
- data/src/index/erb.h +9 -0
- data/src/index/haml.c +334 -0
- data/src/index/haml.h +10 -0
- data/src/index/indexer.c +702 -0
- data/src/index/indexer.h +19 -0
- data/src/index/walk.c +144 -0
- data/src/index/walk.h +14 -0
- data/src/main.c +81 -0
- data/src/output/baseline.c +101 -0
- data/src/output/baseline.h +15 -0
- data/src/output/report.c +440 -0
- data/src/output/report.h +28 -0
- metadata +231 -0
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
#include "prism/string_query.h"
|
|
2
|
+
|
|
3
|
+
#include "prism/internal/assert.h"
|
|
4
|
+
#include "prism/internal/char.h"
|
|
5
|
+
#include "prism/internal/encoding.h"
|
|
6
|
+
#include "prism/internal/options.h"
|
|
7
|
+
#include "prism/internal/parser.h"
|
|
8
|
+
|
|
9
|
+
#include <string.h>
|
|
10
|
+
|
|
11
|
+
/** The category of slice returned from pm_slice_type. */
|
|
12
|
+
typedef enum {
|
|
13
|
+
/** Returned when the given version is invalid. */
|
|
14
|
+
PM_SLICE_TYPE_ERROR_VERSION = -2,
|
|
15
|
+
|
|
16
|
+
/** Returned when the given encoding name is invalid. */
|
|
17
|
+
PM_SLICE_TYPE_ERROR = -1,
|
|
18
|
+
|
|
19
|
+
/** Returned when no other types apply to the slice. */
|
|
20
|
+
PM_SLICE_TYPE_NONE,
|
|
21
|
+
|
|
22
|
+
/** Returned when the slice is a valid local variable name. */
|
|
23
|
+
PM_SLICE_TYPE_LOCAL,
|
|
24
|
+
|
|
25
|
+
/** Returned when the slice is a valid constant name. */
|
|
26
|
+
PM_SLICE_TYPE_CONSTANT,
|
|
27
|
+
|
|
28
|
+
/** Returned when the slice is a valid method name. */
|
|
29
|
+
PM_SLICE_TYPE_METHOD_NAME
|
|
30
|
+
} pm_slice_type_t;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Check that the slice is a valid local variable name or constant, in the
|
|
34
|
+
* given version of Ruby. The version is read as pm_options_version_set reads
|
|
35
|
+
* it, and NULL is the latest version.
|
|
36
|
+
*/
|
|
37
|
+
static pm_slice_type_t
|
|
38
|
+
pm_slice_type(const uint8_t *source, size_t length, const char *encoding_name, const char *version) {
|
|
39
|
+
// first, get the right encoding object
|
|
40
|
+
const pm_encoding_t *encoding = pm_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name)));
|
|
41
|
+
if (encoding == NULL) return PM_SLICE_TYPE_ERROR;
|
|
42
|
+
|
|
43
|
+
/* Then the version of Ruby, which decides which letters are uppercase. */
|
|
44
|
+
pm_options_version_t target;
|
|
45
|
+
if (!pm_options_version_parse(version, version == NULL ? 0 : strlen(version), &target)) return PM_SLICE_TYPE_ERROR_VERSION;
|
|
46
|
+
|
|
47
|
+
// check that there is at least one character
|
|
48
|
+
if (length == 0) return PM_SLICE_TYPE_NONE;
|
|
49
|
+
|
|
50
|
+
/* A name starts with an ASCII letter, an underscore, or any valid character
|
|
51
|
+
* that is not ASCII. */
|
|
52
|
+
size_t width;
|
|
53
|
+
if (*source >= 0x80) {
|
|
54
|
+
if ((width = pm_encoding_char_width(encoding, source, (ptrdiff_t) length)) == 0) return PM_SLICE_TYPE_NONE;
|
|
55
|
+
} else if (pm_encoding_ascii_alpha_p(*source) || (*source == '_')) {
|
|
56
|
+
width = 1;
|
|
57
|
+
} else {
|
|
58
|
+
return PM_SLICE_TYPE_NONE;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// determine the type of the slice based on the first character
|
|
62
|
+
const uint8_t *end = source + length;
|
|
63
|
+
pm_slice_type_t result = pm_encoding_isupper_char(encoding, source, end - source, target) ? PM_SLICE_TYPE_CONSTANT : PM_SLICE_TYPE_LOCAL;
|
|
64
|
+
|
|
65
|
+
/* The rest of a name is ASCII letters, digits, and underscores, and any
|
|
66
|
+
* valid characters that are not ASCII. */
|
|
67
|
+
source += width;
|
|
68
|
+
|
|
69
|
+
while (source < end) {
|
|
70
|
+
if (*source >= 0x80) {
|
|
71
|
+
if ((width = pm_encoding_char_width(encoding, source, end - source)) == 0) break;
|
|
72
|
+
source += width;
|
|
73
|
+
} else if (pm_encoding_ascii_alnum_p(*source) || (*source == '_')) {
|
|
74
|
+
source++;
|
|
75
|
+
} else {
|
|
76
|
+
break;
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// accept a ! or ? at the end of the slice as a method name
|
|
81
|
+
if (source < end && (*source == '!' || *source == '?' || *source == '=')) {
|
|
82
|
+
source++;
|
|
83
|
+
result = PM_SLICE_TYPE_METHOD_NAME;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// valid if we are at the end of the slice
|
|
87
|
+
return source == end ? result : PM_SLICE_TYPE_NONE;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Check that the slice is a valid local variable name.
|
|
92
|
+
*/
|
|
93
|
+
pm_string_query_t
|
|
94
|
+
pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name, const char *version) {
|
|
95
|
+
switch (pm_slice_type(source, length, encoding_name, version)) {
|
|
96
|
+
case PM_SLICE_TYPE_ERROR_VERSION:
|
|
97
|
+
return PM_STRING_QUERY_ERROR_VERSION;
|
|
98
|
+
case PM_SLICE_TYPE_ERROR:
|
|
99
|
+
return PM_STRING_QUERY_ERROR;
|
|
100
|
+
case PM_SLICE_TYPE_NONE:
|
|
101
|
+
case PM_SLICE_TYPE_CONSTANT:
|
|
102
|
+
case PM_SLICE_TYPE_METHOD_NAME:
|
|
103
|
+
return PM_STRING_QUERY_FALSE;
|
|
104
|
+
case PM_SLICE_TYPE_LOCAL:
|
|
105
|
+
return PM_STRING_QUERY_TRUE;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
PRISM_ASSERT(false && "unreachable");
|
|
109
|
+
return PM_STRING_QUERY_FALSE;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Check that the slice is a valid constant name.
|
|
114
|
+
*/
|
|
115
|
+
pm_string_query_t
|
|
116
|
+
pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name, const char *version) {
|
|
117
|
+
switch (pm_slice_type(source, length, encoding_name, version)) {
|
|
118
|
+
case PM_SLICE_TYPE_ERROR_VERSION:
|
|
119
|
+
return PM_STRING_QUERY_ERROR_VERSION;
|
|
120
|
+
case PM_SLICE_TYPE_ERROR:
|
|
121
|
+
return PM_STRING_QUERY_ERROR;
|
|
122
|
+
case PM_SLICE_TYPE_NONE:
|
|
123
|
+
case PM_SLICE_TYPE_LOCAL:
|
|
124
|
+
case PM_SLICE_TYPE_METHOD_NAME:
|
|
125
|
+
return PM_STRING_QUERY_FALSE;
|
|
126
|
+
case PM_SLICE_TYPE_CONSTANT:
|
|
127
|
+
return PM_STRING_QUERY_TRUE;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
PRISM_ASSERT(false && "unreachable");
|
|
131
|
+
return PM_STRING_QUERY_FALSE;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Check that the slice is a valid method name.
|
|
136
|
+
*/
|
|
137
|
+
pm_string_query_t
|
|
138
|
+
pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name, const char *version) {
|
|
139
|
+
#define B(p) ((p) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE)
|
|
140
|
+
#define C1(c) (*source == c)
|
|
141
|
+
#define C2(s) (memcmp(source, s, 2) == 0)
|
|
142
|
+
#define C3(s) (memcmp(source, s, 3) == 0)
|
|
143
|
+
|
|
144
|
+
switch (pm_slice_type(source, length, encoding_name, version)) {
|
|
145
|
+
case PM_SLICE_TYPE_ERROR_VERSION:
|
|
146
|
+
return PM_STRING_QUERY_ERROR_VERSION;
|
|
147
|
+
case PM_SLICE_TYPE_ERROR:
|
|
148
|
+
return PM_STRING_QUERY_ERROR;
|
|
149
|
+
case PM_SLICE_TYPE_NONE:
|
|
150
|
+
break;
|
|
151
|
+
case PM_SLICE_TYPE_LOCAL:
|
|
152
|
+
// numbered parameters are not valid method names
|
|
153
|
+
return B((length != 2) || (source[0] != '_') || (source[1] == '0') || !pm_char_is_decimal_digit(source[1]));
|
|
154
|
+
case PM_SLICE_TYPE_CONSTANT:
|
|
155
|
+
// all constants are valid method names
|
|
156
|
+
case PM_SLICE_TYPE_METHOD_NAME:
|
|
157
|
+
// all method names are valid method names
|
|
158
|
+
return PM_STRING_QUERY_TRUE;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
switch (length) {
|
|
162
|
+
case 1:
|
|
163
|
+
return B(C1('&') || C1('`') || C1('!') || C1('^') || C1('>') || C1('<') || C1('-') || C1('%') || C1('|') || C1('+') || C1('/') || C1('*') || C1('~'));
|
|
164
|
+
case 2:
|
|
165
|
+
return B(C2("!=") || C2("!~") || C2("[]") || C2("==") || C2("=~") || C2(">=") || C2(">>") || C2("<=") || C2("<<") || C2("**"));
|
|
166
|
+
case 3:
|
|
167
|
+
return B(C3("===") || C3("<=>") || C3("[]="));
|
|
168
|
+
default:
|
|
169
|
+
return PM_STRING_QUERY_FALSE;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
#undef B
|
|
173
|
+
#undef C1
|
|
174
|
+
#undef C2
|
|
175
|
+
#undef C3
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Check that the slice is a keyword. Keywords are spelled in ASCII, so any
|
|
180
|
+
* ASCII-compatible encoding spells them with the same bytes. Every version of
|
|
181
|
+
* Ruby that prism supports has the same keywords, but the version must still
|
|
182
|
+
* be valid.
|
|
183
|
+
*/
|
|
184
|
+
pm_string_query_t
|
|
185
|
+
pm_string_query_keyword(const uint8_t *source, size_t length, const char *encoding_name, const char *version) {
|
|
186
|
+
if (pm_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name))) == NULL) {
|
|
187
|
+
return PM_STRING_QUERY_ERROR;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (version != NULL && !pm_options_version_supported_p(version, strlen(version))) {
|
|
191
|
+
return PM_STRING_QUERY_ERROR_VERSION;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
return pm_keyword_p(source, source + length) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE;
|
|
195
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
#include "prism/internal/stringy.h"
|
|
2
|
+
|
|
3
|
+
#include "prism/internal/assert.h"
|
|
4
|
+
|
|
5
|
+
#include <string.h>
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Initialize a shared string that is based on initial input.
|
|
9
|
+
*/
|
|
10
|
+
void
|
|
11
|
+
pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) {
|
|
12
|
+
PRISM_ASSERT(start <= end);
|
|
13
|
+
|
|
14
|
+
*string = (pm_string_t) {
|
|
15
|
+
.source = start,
|
|
16
|
+
.length = (size_t) (end - start)
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Initialize a string that views the given bytes.
|
|
22
|
+
*/
|
|
23
|
+
void
|
|
24
|
+
pm_string_init(pm_string_t *string, const uint8_t *source, size_t length) {
|
|
25
|
+
*string = (pm_string_t) { .source = source, .length = length };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Compare the underlying lengths and bytes of two strings. Returns 0 if the
|
|
30
|
+
* strings are equal, a negative number if the left string is less than the
|
|
31
|
+
* right string, and a positive number if the left string is greater than the
|
|
32
|
+
* right string.
|
|
33
|
+
*/
|
|
34
|
+
int
|
|
35
|
+
pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
|
|
36
|
+
size_t left_length = pm_string_length(left);
|
|
37
|
+
size_t right_length = pm_string_length(right);
|
|
38
|
+
|
|
39
|
+
if (left_length < right_length) {
|
|
40
|
+
return -1;
|
|
41
|
+
} else if (left_length > right_length) {
|
|
42
|
+
return 1;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
return memcmp(pm_string_source(left), pm_string_source(right), left_length);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Returns true if every byte of the string is ASCII.
|
|
50
|
+
*/
|
|
51
|
+
bool
|
|
52
|
+
pm_string_ascii_only_p(const pm_string_t *string) {
|
|
53
|
+
const uint8_t *source = pm_string_source(string);
|
|
54
|
+
size_t length = pm_string_length(string);
|
|
55
|
+
|
|
56
|
+
for (size_t index = 0; index < length; index++) {
|
|
57
|
+
if (source[index] >= 0x80) return false;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
return true;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Returns the length associated with the string.
|
|
65
|
+
*/
|
|
66
|
+
size_t
|
|
67
|
+
pm_string_length(const pm_string_t *string) {
|
|
68
|
+
return string->length;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns the start pointer associated with the string.
|
|
73
|
+
*/
|
|
74
|
+
const uint8_t *
|
|
75
|
+
pm_string_source(const pm_string_t *string) {
|
|
76
|
+
return string->source;
|
|
77
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#include "prism/internal/strncasecmp.h"
|
|
2
|
+
|
|
3
|
+
#include "prism/compiler/inline.h"
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* A locale-insensitive version of `tolower(3)`
|
|
7
|
+
*/
|
|
8
|
+
static PRISM_INLINE int
|
|
9
|
+
pm_tolower(int c) {
|
|
10
|
+
if ('A' <= c && c <= 'Z') {
|
|
11
|
+
return c | 0x20;
|
|
12
|
+
}
|
|
13
|
+
return c;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Compare two strings, ignoring case, up to the given length. Returns 0 if the
|
|
18
|
+
* strings are equal, a negative number if string1 is less than string2, or a
|
|
19
|
+
* positive number if string1 is greater than string2.
|
|
20
|
+
*
|
|
21
|
+
* Note that this is effectively our own implementation of strncasecmp, but it's
|
|
22
|
+
* not available on all of the platforms we want to support so we're rolling it
|
|
23
|
+
* here.
|
|
24
|
+
*/
|
|
25
|
+
int
|
|
26
|
+
pm_strncasecmp(const uint8_t *string1, const uint8_t *string2, size_t length) {
|
|
27
|
+
size_t offset = 0;
|
|
28
|
+
int difference = 0;
|
|
29
|
+
|
|
30
|
+
while (offset < length) {
|
|
31
|
+
if ((difference = pm_tolower(string1[offset]) - pm_tolower(string2[offset])) != 0) return difference;
|
|
32
|
+
if (string1[offset] == '\0') return 0;
|
|
33
|
+
offset++;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return difference;
|
|
37
|
+
}
|