11#include < algorithm>
22#include < cassert>
33#include < cctype>
4- #include < cinttypes>
54#include < cstdarg>
65#include < cstdio>
76#include < functional>
8- #include < iostream>
97#include < map>
10- #include < set>
118#include < string>
129#include < string_view>
1310#include < vector>
@@ -72,42 +69,24 @@ size_t GetFileSize(const std::string& filename, int* error) {
7269 return result;
7370}
7471
75- bool EndsWith (const std::string& str, std::string_view suffix) {
76- size_t suffix_len = suffix.length ();
77- size_t str_len = str.length ();
78- if (str_len < suffix_len) {
79- return false ;
80- }
81- return str.compare (str_len - suffix_len, suffix_len, suffix) == 0 ;
82- }
83-
84- bool StartsWith (const std::string& str, std::string_view prefix) {
85- size_t prefix_len = prefix.length ();
86- size_t str_len = str.length ();
87- if (str_len < prefix_len) {
88- return false ;
89- }
90- return str.compare (0 , prefix_len, prefix) == 0 ;
91- }
92-
93- bool FilenameIsConfigGypi (const std::string& path) {
94- return path == " config.gypi" || EndsWith (path, " /config.gypi" );
72+ constexpr bool FilenameIsConfigGypi (const std::string_view path) {
73+ return path == " config.gypi" || path.ends_with (" /config.gypi" );
9574}
9675
9776typedef std::vector<std::string> FileList;
9877typedef std::map<std::string, FileList> FileMap;
9978
10079bool SearchFiles (const std::string& dir,
10180 FileMap* file_map,
102- const std::string& extension) {
81+ std::string_view extension) {
10382 uv_fs_t scan_req;
10483 int result = uv_fs_scandir (nullptr , &scan_req, dir.c_str (), 0 , nullptr );
10584 bool errored = false ;
10685 if (result < 0 ) {
10786 PrintUvError (" scandir" , dir.c_str (), result);
10887 errored = true ;
10988 } else {
110- auto it = file_map->insert ({extension, FileList ()}).first ;
89+ auto it = file_map->insert ({std::string ( extension) , FileList ()}).first ;
11190 FileList& files = it->second ;
11291 files.reserve (files.size () + result);
11392 uv_dirent_t dent;
@@ -124,7 +103,7 @@ bool SearchFiles(const std::string& dir,
124103 }
125104
126105 std::string path = dir + ' /' + dent.name ;
127- if (EndsWith ( path, extension)) {
106+ if (path. ends_with ( extension)) {
128107 files.emplace_back (path);
129108 continue ;
130109 }
@@ -153,12 +132,11 @@ constexpr std::string_view kJsSuffix = ".js";
153132constexpr std::string_view kGypiSuffix = " .gypi" ;
154133constexpr std::string_view depsPrefix = " deps/" ;
155134constexpr std::string_view libPrefix = " lib/" ;
156- std::set<std::string_view> kAllowedExtensions {
157- kGypiSuffix , kJsSuffix , kMjsSuffix };
158135
159- std::string_view HasAllowedExtensions (const std::string& filename) {
160- for (const auto & ext : kAllowedExtensions ) {
161- if (EndsWith (filename, ext)) {
136+ constexpr std::string_view HasAllowedExtensions (
137+ const std::string_view filename) {
138+ for (const auto & ext : {kGypiSuffix , kJsSuffix , kMjsSuffix }) {
139+ if (filename.ends_with (ext)) {
162140 return ext;
163141 }
164142 }
@@ -350,17 +328,17 @@ std::string GetFileId(const std::string& filename) {
350328 size_t start = 0 ;
351329 std::string prefix;
352330 // Strip .mjs and .js suffix
353- if (EndsWith ( filename, kMjsSuffix )) {
331+ if (filename. ends_with ( kMjsSuffix )) {
354332 end -= kMjsSuffix .size ();
355- } else if (EndsWith ( filename, kJsSuffix )) {
333+ } else if (filename. ends_with ( kJsSuffix )) {
356334 end -= kJsSuffix .size ();
357335 }
358336
359337 // deps/acorn/acorn/dist/acorn.js -> internal/deps/acorn/acorn/dist/acorn
360- if (StartsWith ( filename, depsPrefix)) {
338+ if (filename. starts_with ( depsPrefix)) {
361339 start = depsPrefix.size ();
362340 prefix = " internal/deps/" ;
363- } else if (StartsWith ( filename, libPrefix)) {
341+ } else if (filename. starts_with ( libPrefix)) {
364342 // lib/internal/url.js -> internal/url
365343 start = libPrefix.size ();
366344 prefix = " " ;
@@ -381,17 +359,16 @@ std::string GetVariableName(const std::string& id) {
381359 return result;
382360}
383361
384- std::vector<std::string> GetCodeTable () {
385- size_t size = 1 << 16 ;
386- std::vector<std::string> code_table (size);
387- for (size_t i = 0 ; i < size; ++i) {
388- code_table[i] = std::to_string (i) + ' ,' ;
362+ static const std::array<std::string, 65536 > GetCodeTable () {
363+ std::array<std::string, 65536 > table{};
364+ for (size_t i = 0 ; i < 65536 ; ++i) {
365+ table[i] = std::to_string (i) + ' ,' ;
389366 }
390- return code_table ;
367+ return table ;
391368}
392369
393- const std::string& GetCode (uint16_t index) {
394- static std::vector <std::string> table = GetCodeTable ();
370+ const std::string_view GetCode (uint16_t index) {
371+ static std::array <std::string, 65536 > table = GetCodeTable ();
395372 return table[index];
396373}
397374
@@ -532,8 +509,7 @@ Fragment GetDefinitionImpl(const std::vector<char>& code,
532509 // Avoid using snprintf on large chunks of data because it's much slower.
533510 // It's fine to use it on small amount of data though.
534511 if constexpr (is_two_byte) {
535- std::vector<uint16_t > utf16_codepoints;
536- utf16_codepoints.resize (count);
512+ std::vector<uint16_t > utf16_codepoints (count);
537513 size_t utf16_count = simdutf::convert_utf8_to_utf16 (
538514 code.data (),
539515 code.size (),
@@ -542,8 +518,8 @@ Fragment GetDefinitionImpl(const std::vector<char>& code,
542518 utf16_codepoints.resize (utf16_count);
543519 Debug (" static size %zu\n " , utf16_count);
544520 for (size_t i = 0 ; i < utf16_count; ++i) {
545- const std::string& str = GetCode (utf16_codepoints[i]);
546- memcpy (result.data () + cur, str.c_str (), str.size ());
521+ std::string_view str = GetCode (utf16_codepoints[i]);
522+ memcpy (result.data () + cur, str.data (), str.size ());
547523 cur += str.size ();
548524 }
549525 } else {
@@ -556,8 +532,8 @@ Fragment GetDefinitionImpl(const std::vector<char>& code,
556532 i,
557533 ch);
558534 }
559- const std::string& str = GetCode (ch);
560- memcpy (result.data () + cur, str.c_str (), str.size ());
535+ std::string_view str = GetCode (ch);
536+ memcpy (result.data () + cur, str.data (), str.size ());
561537 cur += str.size ();
562538 }
563539 }
@@ -895,8 +871,8 @@ int Main(int argc, char* argv[]) {
895871 int error = 0 ;
896872 const std::string& file = args[i];
897873 if (IsDirectory (file, &error)) {
898- if (!SearchFiles (file, &file_map, std::string ( kJsSuffix ) ) ||
899- !SearchFiles (file, &file_map, std::string ( kMjsSuffix ) )) {
874+ if (!SearchFiles (file, &file_map, kJsSuffix ) ||
875+ !SearchFiles (file, &file_map, kMjsSuffix )) {
900876 return 1 ;
901877 }
902878 } else if (error != 0 ) {
0 commit comments