From: Markus Triska Date: Sun, 27 Aug 2023 07:56:30 +0000 (+0200) Subject: ENHANCED: rudimentary implementation of normalize_space X-Git-Tag: v0.9.2~2^2~1 X-Git-Url: https://git.sagredo.dev/?a=commitdiff_plain;h=9b96735615fa4f698454fe9a85d1022209dc6c1a;p=scryer-prolog.git ENHANCED: rudimentary implementation of normalize_space This removes leading and trailing whitespace from the text. Whitespace within the string is not yet considered. --- diff --git a/src/lib/xpath.pl b/src/lib/xpath.pl index a871f78d..4ec01203 100644 --- a/src/lib/xpath.pl +++ b/src/lib/xpath.pl @@ -95,7 +95,8 @@ op(200, fy, @) ]). -:- use_module(library(lists),[member/2,memberchk/2]). +:- use_module(library(lists),[member/2,memberchk/2,reverse/2]). +:- use_module(library(charsio)). :- use_module(library(error)). :- use_module(library(dcgs)). :- use_module(library(si)). @@ -636,5 +637,16 @@ text_of_1([C|Cs]) --> seq([C|Cs]). xsd_number_chars(Number, Chars) :- number_chars(Number, Chars). -normalize_space(Text0, Text) :- - Text0 = Text. % no conversion for the moment. +normalize_space(Cs0, Cs) :- + must_be(chars, Cs0), + no_leading_whitespace(Cs0, Cs1), + reverse(Cs1, Cs2), + no_leading_whitespace(Cs2, Cs3), + reverse(Cs3, Cs). + +no_leading_whitespace([], []). +no_leading_whitespace([C0|Cs0], Cs) :- + ( char_type(C0, whitespace) -> + no_leading_whitespace(Cs0, Cs) + ; Cs = [C0|Cs0] + ).