Ruby 4.1.0dev (2026-12-06 revision 6b501f5444cf675f4a654bda46c812e2c1df9fb8)
string_query.c
2
3#include "prism/internal/assert.h"
4#include "prism/internal/char.h"
5#include "prism/internal/encoding.h"
6#include "prism/internal/memequal.h"
7#include "prism/internal/options.h"
8#include "prism/internal/parser.h"
9
10#include <string.h>
11
13typedef enum {
15 PM_SLICE_TYPE_ERROR_VERSION = -2,
16
18 PM_SLICE_TYPE_ERROR = -1,
19
21 PM_SLICE_TYPE_NONE,
22
24 PM_SLICE_TYPE_LOCAL,
25
27 PM_SLICE_TYPE_CONSTANT,
28
30 PM_SLICE_TYPE_METHOD_NAME
31} pm_slice_type_t;
32
38static pm_slice_type_t
39pm_slice_type(const uint8_t *source, size_t length, const char *encoding_name, size_t encoding_length, const char *version, size_t version_length) {
40 // first, get the right encoding object
41 const uint8_t *name = (const uint8_t *) encoding_name;
42 const pm_encoding_t *encoding = pm_encoding_find(name, name + encoding_length);
43 if (encoding == NULL) return PM_SLICE_TYPE_ERROR;
44
45 /* Then the version of Ruby, which decides which letters are uppercase. */
46 pm_options_version_t target;
47 if (!pm_options_version_parse(version, version_length, &target)) return PM_SLICE_TYPE_ERROR_VERSION;
48
49 // check that there is at least one character
50 if (length == 0) return PM_SLICE_TYPE_NONE;
51
52 /* A name starts with an ASCII letter, an underscore, or any valid character
53 * that is not ASCII. */
54 size_t width;
55 if (*source >= 0x80) {
56 if ((width = pm_encoding_char_width(encoding, source, (ptrdiff_t) length)) == 0) return PM_SLICE_TYPE_NONE;
57 } else if (pm_encoding_ascii_alpha_p(*source) || (*source == '_')) {
58 width = 1;
59 } else {
60 return PM_SLICE_TYPE_NONE;
61 }
62
63 // determine the type of the slice based on the first character
64 const uint8_t *end = source + length;
65 pm_slice_type_t result = pm_encoding_isupper_char(encoding, source, end - source, target) ? PM_SLICE_TYPE_CONSTANT : PM_SLICE_TYPE_LOCAL;
66
67 /* The rest of a name is ASCII letters, digits, and underscores, and any
68 * valid characters that are not ASCII. */
69 source += width;
70
71 while (source < end) {
72 if (*source >= 0x80) {
73 if ((width = pm_encoding_char_width(encoding, source, end - source)) == 0) break;
74 source += width;
75 } else if (pm_encoding_ascii_alnum_p(*source) || (*source == '_')) {
76 source++;
77 } else {
78 break;
79 }
80 }
81
82 // accept a ! or ? at the end of the slice as a method name
83 if (source < end && (*source == '!' || *source == '?' || *source == '=')) {
84 source++;
85 result = PM_SLICE_TYPE_METHOD_NAME;
86 }
87
88 // valid if we are at the end of the slice
89 return source == end ? result : PM_SLICE_TYPE_NONE;
90}
91
96pm_string_query_local(const uint8_t *source, size_t length, const char *encoding_name, size_t encoding_length, const char *version, size_t version_length) {
97 switch (pm_slice_type(source, length, encoding_name, encoding_length, version, version_length)) {
98 case PM_SLICE_TYPE_ERROR_VERSION:
100 case PM_SLICE_TYPE_ERROR:
102 case PM_SLICE_TYPE_NONE:
103 case PM_SLICE_TYPE_CONSTANT:
104 case PM_SLICE_TYPE_METHOD_NAME:
106 case PM_SLICE_TYPE_LOCAL:
108 }
109
110 PRISM_ASSERT(false && "unreachable");
112}
113
118pm_string_query_constant(const uint8_t *source, size_t length, const char *encoding_name, size_t encoding_length, const char *version, size_t version_length) {
119 switch (pm_slice_type(source, length, encoding_name, encoding_length, version, version_length)) {
120 case PM_SLICE_TYPE_ERROR_VERSION:
122 case PM_SLICE_TYPE_ERROR:
124 case PM_SLICE_TYPE_NONE:
125 case PM_SLICE_TYPE_LOCAL:
126 case PM_SLICE_TYPE_METHOD_NAME:
128 case PM_SLICE_TYPE_CONSTANT:
130 }
131
132 PRISM_ASSERT(false && "unreachable");
134}
135
140pm_string_query_method_name(const uint8_t *source, size_t length, const char *encoding_name, size_t encoding_length, const char *version, size_t version_length) {
141#define B(p) ((p) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE)
142#define C1(c) (*source == c)
143#define C2(s) pm_memequal(source, s, 2)
144#define C3(s) pm_memequal(source, s, 3)
145
146 switch (pm_slice_type(source, length, encoding_name, encoding_length, version, version_length)) {
147 case PM_SLICE_TYPE_ERROR_VERSION:
149 case PM_SLICE_TYPE_ERROR:
151 case PM_SLICE_TYPE_NONE:
152 break;
153 case PM_SLICE_TYPE_LOCAL:
154 // numbered parameters are not valid method names
155 return B((length != 2) || (source[0] != '_') || (source[1] == '0') || !pm_char_is_decimal_digit(source[1]));
156 case PM_SLICE_TYPE_CONSTANT:
157 // all constants are valid method names
158 case PM_SLICE_TYPE_METHOD_NAME:
159 // all method names are valid method names
161 }
162
163 switch (length) {
164 case 1:
165 return B(C1('&') || C1('`') || C1('!') || C1('^') || C1('>') || C1('<') || C1('-') || C1('%') || C1('|') || C1('+') || C1('/') || C1('*') || C1('~'));
166 case 2:
167 return B(C2("!=") || C2("!~") || C2("[]") || C2("==") || C2("=~") || C2(">=") || C2(">>") || C2("<=") || C2("<<") || C2("**"));
168 case 3:
169 return B(C3("===") || C3("<=>") || C3("[]="));
170 default:
172 }
173
174#undef B
175#undef C1
176#undef C2
177#undef C3
178}
179
187pm_string_query_keyword(const uint8_t *source, size_t length, const char *encoding_name, size_t encoding_length, const char *version, size_t version_length) {
188 const uint8_t *name = (const uint8_t *) encoding_name;
189 if (pm_encoding_find(name, name + encoding_length) == NULL) {
191 }
192
193 if (version != NULL && !pm_options_version_supported_p(version, version_length)) {
195 }
196
197 return pm_keyword_p(source, source + length) ? PM_STRING_QUERY_TRUE : PM_STRING_QUERY_FALSE;
198}
Functions for querying properties of strings, such as whether they are valid local variable names,...
pm_string_query_t
Represents the results of a slice query.
@ PM_STRING_QUERY_TRUE
Returned if the result of the slice query is true.
@ PM_STRING_QUERY_ERROR
Returned if the encoding given to a slice query was invalid.
@ PM_STRING_QUERY_FALSE
Returned if the result of the slice query is false.
@ PM_STRING_QUERY_ERROR_VERSION
Returned if the version given to a slice query was invalid.