Skip to content

Commit

Permalink
implement SPARQL function STRLEN
Browse files Browse the repository at this point in the history
  • Loading branch information
pchampin committed Nov 28, 2024
1 parent a2109a4 commit cc1d4f2
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
16 changes: 15 additions & 1 deletion sparql/src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ pub fn call_function(function: &Function, mut arguments: Vec<EvalResult>) -> Opt
),
_ => unreachable!(),
},
StrLen => todo("StrLen"),
StrLen => {
let [string] = &arguments[..] else {
unreachable!();
};
Some(str_len(string.as_string_lit()?.0))
}
Replace => todo("Replace"),
UCase => todo("UCase"),
LCase => todo("LCase"),
Expand Down Expand Up @@ -315,6 +320,15 @@ pub fn sub_str(
Some(EvalResult::from((Arc::from(&lex[s..e]), tag.cloned())))
}

pub fn str_len(string: &Arc<str>) -> EvalResult {
let l = string.len();
if l <= isize::MAX as usize {
SparqlNumber::from(l as isize).into()
} else {
todo!()
}
}

pub fn triple(s: &EvalResult, p: &EvalResult, o: &EvalResult) -> Option<EvalResult> {
let EvalResult::Term(s) = s else { return None };
let EvalResult::Term(p) = p else { return None };
Expand Down
11 changes: 11 additions & 0 deletions sparql/src/function/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,17 @@ fn sub_str(source: &str, start: f64, length: Option<f64>, exp: Option<&str>) ->
Ok(())
}

#[test_case("foobar", 6)]
#[test_case("foobar@en", 6)]
fn str_len(string: &str, exp: isize) -> TestResult {
let pair = txt2pair(string);
let string = &pair.0;
let source = (&pair.0, pair.1.as_ref());
let exp = EvalResult::from(SparqlNumber::from(exp));
assert!(eval_eq(Some(super::str_len(string)), Some(exp)));
Ok(())
}

#[test_case("<tag:s>", "<tag:p>", "<tag:o>", true)]
#[test_case("<tag:s>", "<tag:p>", "bnode()", true)]
#[test_case("<tag:s>", "<tag:p>", " \"o\" ", true)]
Expand Down
7 changes: 7 additions & 0 deletions sparql/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,13 @@ fn test_expr_variable() -> TestResult {
#[test_case("subStr(\"foobar\", 2, \"42\")", ""; "subStr/3 for string as end")]
#[test_case("subStr(\"foobar\", 2, \"42\"@en)", ""; "subStr/3 for language string as end")]
#[test_case("subStr(\"foobar\", 2, << <tag:s> <tag:p> <tag:o> >>)", ""; "subStr/3 for triple as end")]
// test strLen
#[test_case("strLen(<tag:x>)", ""; "strLen for IRI")]
#[test_case("strLen(bnode())", ""; "strLen for bnode")]
#[test_case("strLen(\"foobar\")", "6"; "strLen for string")]
#[test_case("strLen(\"foobar\"@en)", "6"; "strLen for language string")]
#[test_case("strLen(42)", ""; "strLen for number")]
#[test_case("strLen(<< <tag:s> <tag:p> <tag:o> >>)", ""; "strLen for triple")]
// TODO test other function calls
// test isIri
#[test_case("isIri(<tag:x>)", "true"; "isIri for IRI")]
Expand Down

0 comments on commit cc1d4f2

Please sign in to comment.