GeezerVison is showing github.com — src: use find instead of char-by-char in FromFilePath() · nodejs/node@9346f15
File tree
Expand file treeCollapse file tree
Open diff view settings
Expand file treeCollapse file tree
Open diff view settings
Collapse file
Copy file name to clipboardExpand all lines: src/node_url.cc +14-5Lines changed: 14 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -417,12 +417,21 @@ void BindingData::RegisterExternalReferences( |
417 | 417 | } |
418 | 418 | } |
419 | 419 |
|
420 | | -std::string FromFilePath(const std::string_view file_path) { |
421 | | - std::string escaped_file_path; |
422 | | - for (size_t i = 0; i < file_path.length(); ++i) { |
423 | | - escaped_file_path += file_path[i]; |
424 | | - if (file_path[i] == '%') escaped_file_path += "25"; |
| 420 | +std::string FromFilePath(std::string_view file_path) { |
| 421 | + // Avoid unnecessary allocations. |
| 422 | + size_t pos = file_path.empty() ? std::string_view::npos : file_path.find('%'); |
| 423 | + if (pos == std::string_view::npos) { |
| 424 | + return ada::href_from_file(file_path); |
425 | 425 | } |
| 426 | + // Escape '%' characters to a temporary string. |
| 427 | + std::string escaped_file_path; |
| 428 | + do { |
| 429 | + escaped_file_path += file_path.substr(0, pos + 1); |
| 430 | + escaped_file_path += "25"; |
| 431 | + file_path = file_path.substr(pos + 1); |
| 432 | + pos = file_path.empty() ? std::string_view::npos : file_path.find('%'); |
| 433 | + } while (pos != std::string_view::npos); |
| 434 | + escaped_file_path += file_path; |
426 | 435 | return ada::href_from_file(escaped_file_path); |
427 | 436 | } |
428 | 437 |
|
|
Collapse file
Copy file name to clipboardExpand all lines: src/node_url.h +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -81,7 +81,7 @@ class BindingData : public SnapshotableObject { |
81 | 81 | std::optional<std::string> base); |
82 | 82 | }; |
83 | 83 |
|
84 | | -std::string FromFilePath(const std::string_view file_path); |
| 84 | +std::string FromFilePath(std::string_view file_path); |
85 | 85 |
|
86 | 86 | } // namespace url |
87 | 87 |
|
|