Skip to content

Commit a9f7f01

Browse files
committed
Make type_name2type_identifier retain leading underscores
Because the resulting partial identifiers are considered to be internal, it is fine for them to start with an underscore.
1 parent 2733b56 commit a9f7f01

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

src/ansi-c/type2name.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,15 +308,15 @@ std::string type_name2type_identifier(const std::string &name)
308308
static const std::regex non_alpha_numeric{"[^A-Za-z0-9\x80-\xff]+"};
309309
return std::regex_replace(identifier, non_alpha_numeric, "_");
310310
};
311-
const auto strip_leading_non_letters = [](const std::string &identifier) {
311+
const auto strip_leading_digits = [](const std::string &identifier) {
312312
static const std::regex identifier_regex{
313-
"[A-Za-z\x80-\xff][A-Za-z0-9_\x80-\xff]*"};
313+
"[A-Za-z\x80-\xff_][A-Za-z0-9_\x80-\xff]*"};
314314
std::smatch match_results;
315315
bool found = std::regex_search(identifier, match_results, identifier_regex);
316316
POSTCONDITION(found);
317317
return match_results.str(0);
318318
};
319-
return strip_leading_non_letters(replace_invalid_characters_with_underscore(
319+
return strip_leading_digits(replace_invalid_characters_with_underscore(
320320
replace_special_characters(name)));
321321
}
322322

unit/ansi-c/type2name.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,19 @@ TEST_CASE(
5050
R"(abcdefghijklmnopqrstuvwxyz{|}~)");
5151
CHECK(
5252
type_name2type_identifier(printable_characters) ==
53-
"ptr_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz_"
53+
"_ptr_0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz_"
5454
"start_sub_end_sub_");
5555
}
5656

5757
TEST_CASE(
58-
"type_name2type_identifier leading digits",
58+
"type_name2type_identifier leading characters",
5959
"[core][ansi-c][type_name2type_identifier]")
6060
{
6161
CHECK(
62-
type_name2type_identifier(
63-
"0123456789_banana_0123456789_split_0123456789") ==
62+
type_name2type_identifier("0123456789banana_0123456789_split_0123456789") ==
6463
"banana_0123456789_split_0123456789");
64+
CHECK(type_name2type_identifier("0123456789_banana") == "_banana");
65+
CHECK(type_name2type_identifier("_0123456789") == "_0123456789");
6566
}
6667

6768
TEST_CASE(

0 commit comments

Comments
 (0)