Skip to content

Commit

Permalink
[15_5] Fix suffix of url with parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii committed Oct 30, 2023
1 parent 17603ed commit 4162588
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 12 deletions.
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

0 comments on commit 4162588

Please sign in to comment.