From 4162588971c8442ef89efb0017000e771aaabc6b Mon Sep 17 00:00:00 2001 From: Darcy Shen Date: Mon, 30 Oct 2023 21:27:23 +0800 Subject: [PATCH] [15_5] Fix suffix of url with parameters --- Kernel/Types/analyze.cpp | 10 +++++++++ Kernel/Types/analyze.hpp | 2 ++ System/Classes/url.cpp | 4 +++- tests/System/Classes/url_test.cpp | 37 ++++++++++++++++++++++--------- 4 files changed, 41 insertions(+), 12 deletions(-) diff --git a/Kernel/Types/analyze.cpp b/Kernel/Types/analyze.cpp index 42706568b..e3988b9a7 100644 --- a/Kernel/Types/analyze.cpp +++ b/Kernel/Types/analyze.cpp @@ -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 a, int pos, string in) { int n= N (in), na= N (a); diff --git a/Kernel/Types/analyze.hpp b/Kernel/Types/analyze.hpp index 66f059957..c75bc405e 100644 --- a/Kernel/Types/analyze.hpp +++ b/Kernel/Types/analyze.hpp @@ -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. * diff --git a/System/Classes/url.cpp b/System/Classes/url.cpp index a7cd963d9..c97522983 100644 --- a/System/Classes/url.cpp +++ b/System/Classes/url.cpp @@ -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 ""; } diff --git a/tests/System/Classes/url_test.cpp b/tests/System/Classes/url_test.cpp index 5a5e8abaa..0513f6559 100644 --- a/tests/System/Classes/url_test.cpp +++ b/tests/System/Classes/url_test.cpp @@ -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") {