Skip to content

Commit

Permalink
[12_23] url: support customized protocol
Browse files Browse the repository at this point in the history
  • Loading branch information
da-liii authored Dec 10, 2023
1 parent 95f371e commit 5176da4
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 40 deletions.
14 changes: 14 additions & 0 deletions Kernel/Types/analyze.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ is_alpha (string s) {
return true;
}

bool
is_alphanum (string s) {
int i;
if (N (s) == 0) return false;
for (i= 0; i < N (s); i++)
if (!(is_alpha (s[i]) || is_digit (s[i]))) return false;
return true;
}

bool
is_locase_alpha (string s) {
int i;
Expand Down Expand Up @@ -1439,6 +1448,11 @@ occurs (string what, string in) {
return search_forwards (what, 0, in) >= 0;
}

bool
contains (string s, string what) {
return search_forwards (what, 0, s) >= 0;
}

int
search_backwards (string s, int pos, string in) {
while (pos >= 0) {
Expand Down
4 changes: 4 additions & 0 deletions Kernel/Types/analyze.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ bool is_iso_upcase (char c);
*/
bool is_alpha (string s);

bool is_alphanum (string s);

/**
* @brief Checks if a string contains only lowercase alphabetic characters.
*
Expand Down Expand Up @@ -706,6 +708,8 @@ int count_occurrences (string what, string in);
*/
bool occurs (string what, string in);

bool contains (string s, string what);

/**
* Finds the length of the longest string that is both a suffix of the first
* string and a prefix of the second string.
Expand Down
30 changes: 22 additions & 8 deletions System/Classes/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -243,15 +243,29 @@ url_default (string name, int type= URL_SYSTEM) {

url
url_general (string name, int type= URL_SYSTEM) {
if (starts (name, "local:")) return file_url (name (6, N (name)));
if (starts (name, "file://")) return file_url (name (7, N (name)));
if (starts (name, "http://")) return http_url (name (7, N (name)));
if (starts (name, "https://")) return https_url (name (8, N (name)));
if (starts (name, "ftp://")) return ftp_url (name (6, N (name)));
if (starts (name, "//")) return blank_url (name (2, N (name)));
if (heuristic_is_path (name, type)) return url_path (name, type);
if (contains (name, "://")) {
array<string> tokens = tokenize (name, "://");
string protocol= tokens[0];
string path = tokens[1];
if (N (tokens) == 2 && is_alphanum (protocol)) {
if (protocol == "file") return file_url (path);
if (protocol == "http") return http_url (path);
if (protocol == "https") return https_url (path);
if (protocol == "ftp") return ftp_url (path);
return url_root (tokens[0]) * url_get_name (tokens[1], type);
}
}
if (starts (name, "local:")) {
return file_url (name (6, N (name)));
}
if (starts (name, "//")) {
return blank_url (name (2, N (name)));
}
if (heuristic_is_path (name, type)) {
return url_path (name, type);
}
if (heuristic_is_default (name, type)) return url_default (name, type);
if (heuristic_is_mingw_default (name, type))
if (os_mingw () && heuristic_is_mingw_default (name, type))
return url_mingw_default (name, type);
if (type != URL_CLEAN_UNIX) {
if (heuristic_is_http (name)) return http_url (name);
Expand Down
99 changes: 67 additions & 32 deletions tests/Kernel/Types/analyze_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "a_lolly_test.hpp"
#include "analyze.hpp"

TEST_CASE ("test is alpha") {
TEST_CASE ("is_alpha") {
for (unsigned char c= 0; c < 255; c++) {
if ((c >= 65 && c <= 90) || (c >= 97 && c <= 122)) {
CHECK (is_alpha (c));
Expand All @@ -12,6 +12,57 @@ TEST_CASE ("test is alpha") {
}
}

TEST_CASE ("is_digit") {
for (unsigned char c= 0; c < 255; c++) {
if (c >= 48 && c <= 57) {
CHECK (is_digit (c));
}
else {
CHECK (!is_digit (c));
}
}
}

TEST_CASE ("is_space") {
for (unsigned char c= 0; c < 255; c++) {
if ((c == 9) || (c == 10) || (c == 13) || (c == 32)) {
CHECK (is_space (c));
}
else {
CHECK (!is_space (c));
}
}
}

TEST_CASE ("is_binary_digit") {
for (unsigned char c= 0; c < 255; c++) {
if ((c == '0') || (c == '1')) {
CHECK (is_binary_digit (c));
}
else {
CHECK (!is_binary_digit (c));
}
}
}

TEST_CASE ("is_alpha") {
CHECK (is_alpha ("a"));
CHECK (is_alpha ("abc"));
CHECK (is_alpha ("Hello"));
CHECK (!is_alpha ("!"));
CHECK (!is_alpha ("abc123"));
CHECK (!is_alpha (""));
}

TEST_CASE ("is_alphanum") {
CHECK (is_alphanum ("s3"));
CHECK (is_alphanum ("ipv6"));
CHECK (is_alphanum ("abc"));
CHECK (is_alphanum ("123"));
CHECK (!is_alphanum (""));
CHECK (!is_alphanum ("!"));
}

TEST_CASE ("cjk_unified_ideographs") {
CHECK (is_cjk_unified_ideographs ("<#4E2D>"));
CHECK (has_cjk_unified_ideographs ("<#4E2D>"));
Expand Down Expand Up @@ -103,44 +154,28 @@ TEST_CASE ("test_read_word") {
CHECK_EQ (i, 0);
}

TEST_CASE ("test_is_digit") {
for (unsigned char c= 0; c < 255; c++) {
if (c >= 48 && c <= 57) {
CHECK (is_digit (c));
}
else {
CHECK (!is_digit (c));
}
}
}

TEST_CASE ("test_is_space") {
for (unsigned char c= 0; c < 255; c++) {
if ((c == 9) || (c == 10) || (c == 13) || (c == 32)) {
CHECK (is_space (c));
}
else {
CHECK (!is_space (c));
}
}
}

TEST_CASE ("test_is_binary_digit") {
for (unsigned char c= 0; c < 255; c++) {
if ((c == '0') || (c == '1')) {
CHECK (is_binary_digit (c));
}
else {
CHECK (!is_binary_digit (c));
}
}
TEST_CASE ("contains/occurs") {
CHECK (contains ("abc", "a"));
CHECK (contains ("abc", "ab"));
CHECK (contains ("abc", "bc"));
CHECK (!contains ("abc", "B"));
CHECK (contains ("", ""));
CHECK (contains ("abc", ""));
CHECK (!contains ("abc", " "));
CHECK (contains ("hello world", " "));
}

TEST_CASE ("replace") {
CHECK_EQ (replace ("a-b", "-", "_") == "a_b", true);
CHECK_EQ (replace ("a-b-c", "-", "_") == "a_b_c", true);
}

TEST_CASE ("tokenize") {
CHECK_EQ (tokenize ("hello world", " "), array<string> ("hello", "world"));
CHECK_EQ (tokenize ("zotero://select/library/items/2AIFJFS7", "://"),
array<string> ("zotero", "select/library/items/2AIFJFS7"));
}

TEST_CASE ("roman_nr") {
SUBCASE ("0-9") {
string_eq (roman_nr (0), "o");
Expand Down
10 changes: 10 additions & 0 deletions tests/System/Classes/url_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,14 @@ TEST_CASE ("as_string") {
#endif
}

TEST_CASE ("unknown protocol like zotero") {
url zotero_u= url_system ("zotero://select/library/items/2AIFJFS7");
string_eq (zotero_u.protocol (), "zotero");
CHECK (!is_or (zotero_u));

url tmfs_u= url_system ("tmfs://git/status");
string_eq (tmfs_u.protocol (), "tmfs");
CHECK (!is_or (tmfs_u));
}

TEST_MEMORY_LEAK_ALL

0 comments on commit 5176da4

Please sign in to comment.