From 9f0324cbc1dfd44ab18dd4e73b65f261b433cd8b Mon Sep 17 00:00:00 2001 From: Lilit0x Date: Sat, 9 Sep 2023 20:09:14 +0100 Subject: [PATCH 1/2] chore: replace string function --- crates/gitql-ast/src/function.rs | 28 ++++++++++++++++++++++++++++ docs/function/functions.md | 2 ++ 2 files changed, 30 insertions(+) diff --git a/crates/gitql-ast/src/function.rs b/crates/gitql-ast/src/function.rs index 7134ad79..9cd6000a 100644 --- a/crates/gitql-ast/src/function.rs +++ b/crates/gitql-ast/src/function.rs @@ -29,6 +29,7 @@ lazy_static! { map.insert("left", text_left); map.insert("datalength", text_datalength); map.insert("char", text_char); + map.insert("replace", text_replace); // Date functions map.insert("current_date", date_current_date); @@ -133,6 +134,13 @@ lazy_static! { result: DataType::Text, }, ); + map.insert( + "replace", + Prototype { + parameters: vec![DataType::Text, DataType::Text, DataType::Text], + result: DataType::Text, + }, + ); // Date functions map.insert( @@ -242,6 +250,26 @@ fn text_char(inputs: Vec) -> Value { return Value::Text("".to_string()); } +fn text_replace(inputs: Vec) -> Value { + let text = inputs[0].as_text(); + let old_string = inputs[1].as_text(); + let new_string = inputs[2].as_text(); + + let mut result = String::new(); + let mut end = 0; + for (begin, matched_part) in text + .to_lowercase() + .match_indices(&old_string.to_lowercase()) + { + result.push_str(&text.get(end..begin).unwrap()); + result.push_str(&new_string); + end = begin + matched_part.len(); + } + result.push_str(&text.get(end..text.len()).unwrap()); + + return Value::Text(result); +} + // Date functions fn date_current_date(_inputs: Vec) -> Value { diff --git a/docs/function/functions.md b/docs/function/functions.md index 90096d43..740f62f7 100644 --- a/docs/function/functions.md +++ b/docs/function/functions.md @@ -18,6 +18,7 @@ note that all functions names are case-insensitive. | LEFT | Text, Number | Text | Extracts a number of characters from a string (starting from left). | | DATALENGTH | Text | Number | Returns the number of bytes used to represent an expression. | | CHAR | Number | Text | Returns the character based on the ASCII code. | +| REPLACE | Text, Text, Text | Text | Replaces all occurrences of a substring within a string, with a new substring. | ### String functions samples @@ -32,6 +33,7 @@ SELECT name, ASCII(name) AS firstCharAscii FROM commits SELECT LEFT("AmrDeveloper", 3) AS extract SELECT DATALENGTH("AmrDeveloper") as bytelength SELECT CHAR(345) AS code +SELECT REPLACE("ABC ABC ABC", "a", "c") as replacedText ``` ### Date functions From cf944a79cf49c2f4ed193e38d2b352e995248121 Mon Sep 17 00:00:00 2001 From: Amr Hesham Date: Tue, 12 Sep 2023 09:37:55 +0300 Subject: [PATCH 2/2] Format functions in function.rs --- crates/gitql-ast/src/function.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/gitql-ast/src/function.rs b/crates/gitql-ast/src/function.rs index ba3da1ed..09a2c2fc 100644 --- a/crates/gitql-ast/src/function.rs +++ b/crates/gitql-ast/src/function.rs @@ -277,7 +277,7 @@ fn text_replace(inputs: Vec) -> Value { return Value::Text(result); } - fn text_substring(inputs: Vec) -> Value { +fn text_substring(inputs: Vec) -> Value { let text = inputs[0].as_text(); //according to the specs, a stirng starts at position 1. //but in Rust, the index of a string starts from 0