Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[15_5] Fix suffix of url with parameters #203

Merged
merged 1 commit into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Kernel/Types/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,16 @@ parse (string s, int& pos, SI*& a, int len) {
* Searching, replacing and pattern matching
******************************************************************************/

int
index_of (string s, char c) {
for (int i= 0; i < N (s); i++) {
if (s[i] == c) {
return i;
}
}
return -1;
}

int
search_forwards (array<string> a, int pos, string in) {
int n= N (in), na= N (a);
Expand Down
2 changes: 2 additions & 0 deletions Kernel/Types/analyze.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,6 +625,8 @@ void parse (string s, int& pos, HN& ret);
void parse (string s, int& pos, SI& ret);
void parse (string s, int& pos, SI*& a, int len);

int index_of (string s, char c);

/**
* Searches for a substring in a string.
*
Expand Down
4 changes: 3 additions & 1 deletion System/Classes/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,7 +346,9 @@ suffix (url u) {
string r= s (i + 1, n);
while ((N (r) > 0) && (r[N (r) - 1] == '~' || r[N (r) - 1] == '#'))
r= r (0, N (r) - 1);
return locase_all (r);
int found= index_of (r, '?');
if (found == -1) return locase_all (r);
else return locase_all (r (0, found));
}
return "";
}
Expand Down
37 changes: 26 additions & 11 deletions tests/System/Classes/url_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,17 +121,32 @@ TEST_CASE ("is_concat on unix and wasm") {
#endif

TEST_CASE ("suffix") {
// empty suffix should work
url no_suffix= url ("/a/b/c/d/no_suffix");
CHECK (is_empty (suffix (no_suffix)));
url no_suffix2= url ("/a/b.c/d/no_suffix");
CHECK (is_empty (suffix (no_suffix2)));

// normal suffix should work
url png= url ("/a/b/c/d.png");
string_eq (suffix (png), "png");
url png2= url ("/a/b.c/d.png");
string_eq (suffix (png2), "png");
SUBCASE ("empty suffix") {
url no_suffix= url ("/a/b/c/d/no_suffix");
CHECK (is_empty (suffix (no_suffix)));
url no_suffix2= url ("/a/b.c/d/no_suffix");
CHECK (is_empty (suffix (no_suffix2)));
}

SUBCASE ("normal suffix") {
url png= url ("/a/b/c/d.png");
string_eq (suffix (png), "png");
url png2= url ("/a/b.c/d.png");
string_eq (suffix (png2), "png");
}

SUBCASE ("normal http url") {
url png= url ("https://name.com/path/to.png");
string_eq (suffix (png), "png");

url jpg= url ("https://name.org/path/to.jpg");
string_eq (suffix (jpg), "jpg");
}

SUBCASE ("http url with paramters") {
url jpg= url ("https://name.cn/path/to.jpg?width=100");
string_eq (suffix (jpg), "jpg");
}
}

TEST_CASE ("as_string") {
Expand Down
Loading