prism 2.0.0.rc1-x86_64-linux-gnu
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/BSDmakefile +58 -0
- data/CHANGELOG.md +870 -0
- data/CODE_OF_CONDUCT.md +76 -0
- data/CONTRIBUTING.md +58 -0
- data/LICENSE.md +7 -0
- data/Makefile +145 -0
- data/README.md +81 -0
- data/config.yml +4752 -0
- data/docs/build_system.md +119 -0
- data/docs/configuration.md +69 -0
- data/docs/cruby_compilation.md +27 -0
- data/docs/design.md +53 -0
- data/docs/encoding.md +121 -0
- data/docs/fuzzing.md +81 -0
- data/docs/heredocs.md +36 -0
- data/docs/javascript.md +133 -0
- data/docs/local_variable_depth.md +229 -0
- data/docs/mapping.md +117 -0
- data/docs/parser_translation.md +24 -0
- data/docs/parsing_rules.md +22 -0
- data/docs/releasing.md +147 -0
- data/docs/relocation.md +32 -0
- data/docs/ripper_translation.md +63 -0
- data/docs/ruby_api.md +45 -0
- data/docs/ruby_parser_translation.md +19 -0
- data/docs/serialization.md +230 -0
- data/docs/testing.md +72 -0
- data/ext/prism/api_node.c +7005 -0
- data/ext/prism/extconf.rb +126 -0
- data/ext/prism/extension.c +1783 -0
- data/ext/prism/extension.h +19 -0
- data/include/prism/arena.h +37 -0
- data/include/prism/ast.h +10371 -0
- data/include/prism/buffer.h +52 -0
- data/include/prism/comments.h +55 -0
- data/include/prism/compiler/accel.h +24 -0
- data/include/prism/compiler/align.h +36 -0
- data/include/prism/compiler/assume.h +25 -0
- data/include/prism/compiler/exported.h +24 -0
- data/include/prism/compiler/fallthrough.h +22 -0
- data/include/prism/compiler/filesystem.h +32 -0
- data/include/prism/compiler/flex_array.h +19 -0
- data/include/prism/compiler/force_inline.h +21 -0
- data/include/prism/compiler/format.h +25 -0
- data/include/prism/compiler/inline.h +17 -0
- data/include/prism/compiler/nodiscard.h +22 -0
- data/include/prism/compiler/nonnull.h +18 -0
- data/include/prism/compiler/unused.h +18 -0
- data/include/prism/constant_pool.h +81 -0
- data/include/prism/diagnostic.h +93 -0
- data/include/prism/errors_format.h +53 -0
- data/include/prism/excludes.h +29 -0
- data/include/prism/integer.h +41 -0
- data/include/prism/internal/allocator.h +68 -0
- data/include/prism/internal/allocator_debug.h +88 -0
- data/include/prism/internal/arena.h +108 -0
- data/include/prism/internal/bit.h +42 -0
- data/include/prism/internal/buffer.h +91 -0
- data/include/prism/internal/char.h +145 -0
- data/include/prism/internal/comments.h +53 -0
- data/include/prism/internal/constant_pool.h +152 -0
- data/include/prism/internal/diagnostic.h +389 -0
- data/include/prism/internal/encoding.h +242 -0
- data/include/prism/internal/integer.h +68 -0
- data/include/prism/internal/isinf.h +16 -0
- data/include/prism/internal/line_offset_list.h +34 -0
- data/include/prism/internal/list.h +62 -0
- data/include/prism/internal/magic_comments.h +23 -0
- data/include/prism/internal/memchr.h +15 -0
- data/include/prism/internal/node.h +38 -0
- data/include/prism/internal/options.h +232 -0
- data/include/prism/internal/parser.h +984 -0
- data/include/prism/internal/regexp.h +26 -0
- data/include/prism/internal/serialize.h +39 -0
- data/include/prism/internal/source.h +72 -0
- data/include/prism/internal/static_literals.h +100 -0
- data/include/prism/internal/stringy.h +35 -0
- data/include/prism/internal/strncasecmp.h +18 -0
- data/include/prism/internal/strpbrk.h +33 -0
- data/include/prism/internal/tokens.h +11 -0
- data/include/prism/json.h +32 -0
- data/include/prism/line_offset_list.h +61 -0
- data/include/prism/magic_comments.h +35 -0
- data/include/prism/node.h +94 -0
- data/include/prism/options.h +354 -0
- data/include/prism/parser.h +404 -0
- data/include/prism/prettyprint.h +31 -0
- data/include/prism/serialize.h +115 -0
- data/include/prism/source.h +155 -0
- data/include/prism/stream.h +28 -0
- data/include/prism/string_query.h +63 -0
- data/include/prism/stringy.h +72 -0
- data/include/prism/version.h +38 -0
- data/include/prism.h +131 -0
- data/lib/prism/3.3/prism.so +0 -0
- data/lib/prism/3.4/prism.so +0 -0
- data/lib/prism/4.0/prism.so +0 -0
- data/lib/prism/compiler.rb +815 -0
- data/lib/prism/desugar_compiler.rb +463 -0
- data/lib/prism/dispatcher.rb +2383 -0
- data/lib/prism/dot_visitor.rb +4848 -0
- data/lib/prism/dsl.rb +1369 -0
- data/lib/prism/ffi.rb +740 -0
- data/lib/prism/inspect_visitor.rb +2425 -0
- data/lib/prism/lex_compat.rb +953 -0
- data/lib/prism/mutation_compiler.rb +780 -0
- data/lib/prism/node.rb +33204 -0
- data/lib/prism/node_ext.rb +504 -0
- data/lib/prism/node_find.rb +215 -0
- data/lib/prism/parse_result/errors.rb +72 -0
- data/lib/prism/parse_result/newlines.rb +423 -0
- data/lib/prism/parse_result.rb +1221 -0
- data/lib/prism/pattern.rb +314 -0
- data/lib/prism/polyfill/append_as_bytes.rb +15 -0
- data/lib/prism/polyfill/byteindex.rb +13 -0
- data/lib/prism/polyfill/scan_byte.rb +14 -0
- data/lib/prism/polyfill/unpack1.rb +14 -0
- data/lib/prism/polyfill/warn.rb +36 -0
- data/lib/prism/reflection.rb +427 -0
- data/lib/prism/relocation.rb +567 -0
- data/lib/prism/serialize.rb +5308 -0
- data/lib/prism/string_query.rb +46 -0
- data/lib/prism/translation/parser/builder.rb +70 -0
- data/lib/prism/translation/parser/compiler.rb +2226 -0
- data/lib/prism/translation/parser/lexer.rb +769 -0
- data/lib/prism/translation/parser.rb +376 -0
- data/lib/prism/translation/parser_current.rb +26 -0
- data/lib/prism/translation/parser_versions.rb +36 -0
- data/lib/prism/translation/ripper/filter.rb +53 -0
- data/lib/prism/translation/ripper/lexer.rb +133 -0
- data/lib/prism/translation/ripper/sexp.rb +118 -0
- data/lib/prism/translation/ripper/shim.rb +7 -0
- data/lib/prism/translation/ripper.rb +4278 -0
- data/lib/prism/translation/ruby_parser.rb +1721 -0
- data/lib/prism/translation.rb +20 -0
- data/lib/prism/visitor.rb +1440 -0
- data/lib/prism.rb +168 -0
- data/prism.gemspec +234 -0
- data/rbi/generated/prism/compiler.rbi +489 -0
- data/rbi/generated/prism/desugar_compiler.rbi +306 -0
- data/rbi/generated/prism/dispatcher.rbi +1138 -0
- data/rbi/generated/prism/dot_visitor.rbi +618 -0
- data/rbi/generated/prism/dsl.rbi +751 -0
- data/rbi/generated/prism/inspect_visitor.rbi +503 -0
- data/rbi/generated/prism/lex_compat.rbi +162 -0
- data/rbi/generated/prism/mutation_compiler.rbi +464 -0
- data/rbi/generated/prism/node.rbi +13681 -0
- data/rbi/generated/prism/node_ext.rbi +259 -0
- data/rbi/generated/prism/node_find.rbi +79 -0
- data/rbi/generated/prism/parse_result/errors.rbi +21 -0
- data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
- data/rbi/generated/prism/parse_result.rbi +764 -0
- data/rbi/generated/prism/pattern.rbi +134 -0
- data/rbi/generated/prism/reflection.rbi +89 -0
- data/rbi/generated/prism/relocation.rbi +329 -0
- data/rbi/generated/prism/serialize.rbi +181 -0
- data/rbi/generated/prism/string_query.rbi +36 -0
- data/rbi/generated/prism/translation.rbi +7 -0
- data/rbi/generated/prism/visitor.rbi +650 -0
- data/rbi/generated/prism.rbi +86 -0
- data/rbi/prism/translation/parser.rbi +11 -0
- data/rbi/prism/translation/parser_versions.rbi +28 -0
- data/rbi/prism/translation/ripper.rbi +15 -0
- data/rbi/rubyvm/node_find.rbi +14 -0
- data/sig/generated/prism/compiler.rbs +492 -0
- data/sig/generated/prism/desugar_compiler.rbs +305 -0
- data/sig/generated/prism/dispatcher.rbs +1292 -0
- data/sig/generated/prism/dot_visitor.rbs +635 -0
- data/sig/generated/prism/dsl.rbs +928 -0
- data/sig/generated/prism/inspect_visitor.rbs +509 -0
- data/sig/generated/prism/lex_compat.rbs +166 -0
- data/sig/generated/prism/mutation_compiler.rbs +464 -0
- data/sig/generated/prism/node.rbs +20160 -0
- data/sig/generated/prism/node_ext.rbs +290 -0
- data/sig/generated/prism/node_find.rbs +90 -0
- data/sig/generated/prism/parse_result/errors.rbs +22 -0
- data/sig/generated/prism/parse_result/newlines.rbs +223 -0
- data/sig/generated/prism/parse_result.rbs +859 -0
- data/sig/generated/prism/pattern.rbs +156 -0
- data/sig/generated/prism/reflection.rbs +101 -0
- data/sig/generated/prism/relocation.rbs +428 -0
- data/sig/generated/prism/serialize.rbs +195 -0
- data/sig/generated/prism/string_query.rbs +36 -0
- data/sig/generated/prism/translation.rbs +8 -0
- data/sig/generated/prism/visitor.rbs +1111 -0
- data/sig/generated/prism.rbs +84 -0
- data/sig/manifest.yaml +2 -0
- data/src/arena.c +117 -0
- data/src/buffer.c +380 -0
- data/src/char.c +289 -0
- data/src/comments.c +454 -0
- data/src/constant_pool.c +406 -0
- data/src/diagnostic.c +886 -0
- data/src/encoding.c +5345 -0
- data/src/errors_format.c +519 -0
- data/src/integer.c +681 -0
- data/src/json.c +5733 -0
- data/src/line_offset_list.c +100 -0
- data/src/list.c +24 -0
- data/src/memchr.c +37 -0
- data/src/node.c +4515 -0
- data/src/options.c +451 -0
- data/src/parser.c +349 -0
- data/src/prettyprint.c +9024 -0
- data/src/prism.c +23739 -0
- data/src/regexp.c +1706 -0
- data/src/serialize.c +2309 -0
- data/src/source.c +518 -0
- data/src/static_literals.c +685 -0
- data/src/string_query.c +166 -0
- data/src/stringy.c +91 -0
- data/src/strncasecmp.c +37 -0
- data/src/strpbrk.c +447 -0
- data/src/tokens.c +724 -0
- metadata +259 -0
data/src/string_query.c
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
#include "prism/string_query.h"
|
|
2
|
+
|
|
3
|
+
#include "prism/internal/char.h"
|
|
4
|
+
#include "prism/internal/encoding.h"
|
|
5
|
+
|
|
6
|
+
#include <assert.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
/** The category of slice returned from pm_slice_type. */
|
|
10
|
+
typedef enum {
|
|
11
|
+
/** Returned when the given encoding name is invalid. */
|
|
12
|
+
PM_SLICE_TYPE_ERROR = -1,
|
|
13
|
+
|
|
14
|
+
/** Returned when no other types apply to the slice. */
|
|
15
|
+
PM_SLICE_TYPE_NONE,
|
|
16
|
+
|
|
17
|
+
/** Returned when the slice is a valid local variable name. */
|
|
18
|
+
PM_SLICE_TYPE_LOCAL,
|
|
19
|
+
|
|
20
|
+
/** Returned when the slice is a valid constant name. */
|
|
21
|
+
PM_SLICE_TYPE_CONSTANT,
|
|
22
|
+
|
|
23
|
+
/** Returned when the slice is a valid method name. */
|
|
24
|
+
PM_SLICE_TYPE_METHOD_NAME
|
|
25
|
+
} pm_slice_type_t;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Check that the slice is a valid local variable name or constant.
|
|
29
|
+
*/
|
|
30
|
+
static pm_slice_type_t
|
|
31
|
+
pm_slice_type(const uint8_t *source, size_t length, const char *encoding_name) {
|
|
32
|
+
// first, get the right encoding object
|
|
33
|
+
const pm_encoding_t *encoding = pm_encoding_find((const uint8_t *) encoding_name, (const uint8_t *) (encoding_name + strlen(encoding_name)));
|
|
34
|
+
if (encoding == NULL) return PM_SLICE_TYPE_ERROR;
|
|
35
|
+
|
|
36
|
+
// check that there is at least one character
|
|
37
|
+
if (length == 0) return PM_SLICE_TYPE_NONE;
|
|
38
|
+
|
|
39
|
+
size_t width;
|
|
40
|
+
if ((width = encoding->alpha_char(source, (ptrdiff_t) length)) != 0) {
|
|
41
|
+
// valid because alphabetical
|
|
42
|
+
} else if (*source == '_') {
|
|
43
|
+
// valid because underscore
|
|
44
|
+
width = 1;
|
|
45
|
+
} else if ((*source >= 0x80) && ((width = encoding->char_width(source, (ptrdiff_t) length)) > 0)) {
|
|
46
|
+
// valid because multibyte
|
|
47
|
+
} else {
|
|
48
|
+
// invalid because no match
|
|
49
|
+
return PM_SLICE_TYPE_NONE;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// determine the type of the slice based on the first character
|
|
53
|
+
const uint8_t *end = source + length;
|
|
54
|
+
pm_slice_type_t result = encoding->isupper_char(source, end - source) ? PM_SLICE_TYPE_CONSTANT : PM_SLICE_TYPE_LOCAL;
|
|
55
|
+
|
|
56
|
+
// next, iterate through all of the bytes of the string to ensure that they
|
|
57
|
+
// are all valid identifier characters
|
|
58
|
+
source += width;
|
|
59
|
+
|
|
60
|
+
while (source < end) {
|
|
61
|
+
if ((width = encoding->alnum_char(source, end - source)) != 0) {
|
|
62
|
+
// valid because alphanumeric
|
|
63
|
+
source += width;
|
|
64
|
+
} else if (*source == '_') {
|
|
65
|
+
// valid because underscore
|
|
66
|
+
source++;
|
|
67
|
+
} else if ((*source >= 0x80) && ((width = encoding->char_width(source, end - source)) > 0)) {
|
|
68
|
+
// valid because multibyte
|
|
69
|
+
source += width;
|
|
70
|
+
} else {
|
|
71
|
+
// invalid because no match
|
|
72
|
+
break;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// accept a ! or ? at the end of the slice as a method name
|
|
77
|
+
if (*source == '!' || *source == '?' || *source == '=') {
|
|
78
|
+
source++;
|
|
79
|
+
result = PM_SLICE_TYPE_METHOD_NAME;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// valid if we are at the end of the slice
|
|
83
|
+
return source == end ? result : PM_SLICE_TYPE_NONE;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Check that the slice is a valid local variable name.
|
|
88
|
+
*/
|
|
89
|
+
pm_string_query_t
|
|
90
|
+
pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name) {
|
|
91
|
+
switch (pm_slice_type(source, length, encoding_name)) {
|
|
92
|
+
case PM_SLICE_TYPE_ERROR:
|
|
93
|
+
return PM_STRING_QUERY_ERROR;
|
|
94
|
+
case PM_SLICE_TYPE_NONE:
|
|
95
|
+
case PM_SLICE_TYPE_CONSTANT:
|
|
96
|
+
case PM_SLICE_TYPE_METHOD_NAME:
|
|
97
|
+
return PM_STRING_QUERY_FALSE;
|
|
98
|
+
case PM_SLICE_TYPE_LOCAL:
|
|
99
|
+
return PM_STRING_QUERY_TRUE;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
assert(false && "unreachable");
|
|
103
|
+
return PM_STRING_QUERY_FALSE;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Check that the slice is a valid constant name.
|
|
108
|
+
*/
|
|
109
|
+
pm_string_query_t
|
|
110
|
+
pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name) {
|
|
111
|
+
switch (pm_slice_type(source, length, encoding_name)) {
|
|
112
|
+
case PM_SLICE_TYPE_ERROR:
|
|
113
|
+
return PM_STRING_QUERY_ERROR;
|
|
114
|
+
case PM_SLICE_TYPE_NONE:
|
|
115
|
+
case PM_SLICE_TYPE_LOCAL:
|
|
116
|
+
case PM_SLICE_TYPE_METHOD_NAME:
|
|
117
|
+
return PM_STRING_QUERY_FALSE;
|
|
118
|
+
case PM_SLICE_TYPE_CONSTANT:
|
|
119
|
+
return PM_STRING_QUERY_TRUE;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
assert(false && "unreachable");
|
|
123
|
+
return PM_STRING_QUERY_FALSE;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Check that the slice is a valid method name.
|
|
128
|
+
*/
|
|
129
|
+
pm_string_query_t
|
|
130
|
+
pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name) {
|
|
131
|
+
#define B(p) ((p) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE)
|
|
132
|
+
#define C1(c) (*source == c)
|
|
133
|
+
#define C2(s) (memcmp(source, s, 2) == 0)
|
|
134
|
+
#define C3(s) (memcmp(source, s, 3) == 0)
|
|
135
|
+
|
|
136
|
+
switch (pm_slice_type(source, length, encoding_name)) {
|
|
137
|
+
case PM_SLICE_TYPE_ERROR:
|
|
138
|
+
return PM_STRING_QUERY_ERROR;
|
|
139
|
+
case PM_SLICE_TYPE_NONE:
|
|
140
|
+
break;
|
|
141
|
+
case PM_SLICE_TYPE_LOCAL:
|
|
142
|
+
// numbered parameters are not valid method names
|
|
143
|
+
return B((length != 2) || (source[0] != '_') || (source[1] == '0') || !pm_char_is_decimal_digit(source[1]));
|
|
144
|
+
case PM_SLICE_TYPE_CONSTANT:
|
|
145
|
+
// all constants are valid method names
|
|
146
|
+
case PM_SLICE_TYPE_METHOD_NAME:
|
|
147
|
+
// all method names are valid method names
|
|
148
|
+
return PM_STRING_QUERY_TRUE;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
switch (length) {
|
|
152
|
+
case 1:
|
|
153
|
+
return B(C1('&') || C1('`') || C1('!') || C1('^') || C1('>') || C1('<') || C1('-') || C1('%') || C1('|') || C1('+') || C1('/') || C1('*') || C1('~'));
|
|
154
|
+
case 2:
|
|
155
|
+
return B(C2("!=") || C2("!~") || C2("[]") || C2("==") || C2("=~") || C2(">=") || C2(">>") || C2("<=") || C2("<<") || C2("**"));
|
|
156
|
+
case 3:
|
|
157
|
+
return B(C3("===") || C3("<=>") || C3("[]="));
|
|
158
|
+
default:
|
|
159
|
+
return PM_STRING_QUERY_FALSE;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
#undef B
|
|
163
|
+
#undef C1
|
|
164
|
+
#undef C2
|
|
165
|
+
#undef C3
|
|
166
|
+
}
|
data/src/stringy.c
ADDED
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
#include "prism/internal/stringy.h"
|
|
2
|
+
|
|
3
|
+
#include "prism/internal/allocator.h"
|
|
4
|
+
|
|
5
|
+
#include <assert.h>
|
|
6
|
+
#include <stdlib.h>
|
|
7
|
+
#include <string.h>
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Initialize a shared string that is based on initial input.
|
|
11
|
+
*/
|
|
12
|
+
void
|
|
13
|
+
pm_string_shared_init(pm_string_t *string, const uint8_t *start, const uint8_t *end) {
|
|
14
|
+
assert(start <= end);
|
|
15
|
+
|
|
16
|
+
*string = (pm_string_t) {
|
|
17
|
+
.type = PM_STRING_SHARED,
|
|
18
|
+
.source = start,
|
|
19
|
+
.length = (size_t) (end - start)
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Initialize an owned string that is responsible for freeing allocated memory.
|
|
25
|
+
*/
|
|
26
|
+
void
|
|
27
|
+
pm_string_owned_init(pm_string_t *string, uint8_t *source, size_t length) {
|
|
28
|
+
*string = (pm_string_t) {
|
|
29
|
+
.type = PM_STRING_OWNED,
|
|
30
|
+
.source = source,
|
|
31
|
+
.length = length
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Initialize a constant string that doesn't own its memory source.
|
|
37
|
+
*/
|
|
38
|
+
void
|
|
39
|
+
pm_string_constant_init(pm_string_t *string, const char *source, size_t length) {
|
|
40
|
+
*string = (pm_string_t) {
|
|
41
|
+
.type = PM_STRING_CONSTANT,
|
|
42
|
+
.source = (const uint8_t *) source,
|
|
43
|
+
.length = length
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Compare the underlying lengths and bytes of two strings. Returns 0 if the
|
|
49
|
+
* strings are equal, a negative number if the left string is less than the
|
|
50
|
+
* right string, and a positive number if the left string is greater than the
|
|
51
|
+
* right string.
|
|
52
|
+
*/
|
|
53
|
+
int
|
|
54
|
+
pm_string_compare(const pm_string_t *left, const pm_string_t *right) {
|
|
55
|
+
size_t left_length = pm_string_length(left);
|
|
56
|
+
size_t right_length = pm_string_length(right);
|
|
57
|
+
|
|
58
|
+
if (left_length < right_length) {
|
|
59
|
+
return -1;
|
|
60
|
+
} else if (left_length > right_length) {
|
|
61
|
+
return 1;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
return memcmp(pm_string_source(left), pm_string_source(right), left_length);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Returns the length associated with the string.
|
|
69
|
+
*/
|
|
70
|
+
size_t
|
|
71
|
+
pm_string_length(const pm_string_t *string) {
|
|
72
|
+
return string->length;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Returns the start pointer associated with the string.
|
|
77
|
+
*/
|
|
78
|
+
const uint8_t *
|
|
79
|
+
pm_string_source(const pm_string_t *string) {
|
|
80
|
+
return string->source;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Free the associated memory of the given string.
|
|
85
|
+
*/
|
|
86
|
+
void
|
|
87
|
+
pm_string_cleanup(pm_string_t *string) {
|
|
88
|
+
if (string->type == PM_STRING_OWNED) {
|
|
89
|
+
xfree_sized((void *) string->source, string->length);
|
|
90
|
+
}
|
|
91
|
+
}
|
data/src/strncasecmp.c
ADDED
|
@@ -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 && string1[offset] != '\0') {
|
|
31
|
+
if (string2[offset] == '\0') return string1[offset];
|
|
32
|
+
if ((difference = pm_tolower(string1[offset]) - pm_tolower(string2[offset])) != 0) return difference;
|
|
33
|
+
offset++;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return difference;
|
|
37
|
+
}
|