File tree Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Expand file tree Collapse file tree 2 files changed +15
-6
lines changed Original file line number Diff line number Diff line change @@ -417,12 +417,21 @@ void BindingData::RegisterExternalReferences(
417417 }
418418}
419419
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) ;
425425 }
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;
426435 return ada::href_from_file (escaped_file_path);
427436}
428437
Original file line number Diff line number Diff line change @@ -81,7 +81,7 @@ class BindingData : public SnapshotableObject {
8181 std::optional<std::string> base);
8282};
8383
84- std::string FromFilePath (const std::string_view file_path);
84+ std::string FromFilePath (std::string_view file_path);
8585
8686} // namespace url
8787
You can’t perform that action at this time.
0 commit comments