Skip to content

Commit c05b7f2

Browse files
tamaroningphilberty
authored andcommitted
libcpp: add function to check XID properties
libcpp/ChangeLog: * charset.cc (check_xid_property):new function to check XID_Start and XID_Continue * include/cpplib.h (check_xid_property):add enum representing XID properties Signed-off-by: Raiki Tamura <[email protected]>
1 parent b533df3 commit c05b7f2

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

libcpp/charset.cc

+36
Original file line numberDiff line numberDiff line change
@@ -1256,6 +1256,42 @@ _cpp_uname2c_uax44_lm2 (const char *name, size_t len, char *canon_name)
12561256
return result;
12571257
}
12581258

1259+
/* Returns flags representing the XID properties of the given codepoint. */
1260+
unsigned int
1261+
check_xid_property (cppchar_t c)
1262+
{
1263+
// fast path for ASCII
1264+
if (c < 0x80)
1265+
{
1266+
if (('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z'))
1267+
return XID_START | XID_CONTINUE;
1268+
if (('0' <= c && c <= '9') || c == '_')
1269+
return XID_CONTINUE;
1270+
}
1271+
1272+
if (c > UCS_LIMIT)
1273+
return 0;
1274+
1275+
int mn, mx, md;
1276+
mn = 0;
1277+
mx = ARRAY_SIZE (ucnranges) - 1;
1278+
while (mx != mn)
1279+
{
1280+
md = (mn + mx) / 2;
1281+
if (c <= ucnranges[md].end)
1282+
mx = md;
1283+
else
1284+
mn = md + 1;
1285+
}
1286+
1287+
unsigned short flags = ucnranges[mn].flags;
1288+
1289+
if (flags & CXX23)
1290+
return XID_START | XID_CONTINUE;
1291+
if (flags & NXX23)
1292+
return XID_CONTINUE;
1293+
return 0;
1294+
}
12591295

12601296
/* Returns 1 if C is valid in an identifier, 2 if C is valid except at
12611297
the start of an identifier, and 0 if C is not valid in an

libcpp/include/cpplib.h

+7
Original file line numberDiff line numberDiff line change
@@ -1602,4 +1602,11 @@ bool cpp_input_conversion_is_trivial (const char *input_charset);
16021602
int cpp_check_utf8_bom (const char *data, size_t data_length);
16031603
bool cpp_valid_utf8_p (const char *data, size_t num_bytes);
16041604

1605+
enum {
1606+
XID_START = 1,
1607+
XID_CONTINUE = 2
1608+
};
1609+
1610+
unsigned int check_xid_property (cppchar_t c);
1611+
16051612
#endif /* ! LIBCPP_CPPLIB_H */

0 commit comments

Comments
 (0)