From f16e0d946308acbbec7aa0591acf8287637039cf Mon Sep 17 00:00:00 2001 From: Arnoud Buzing Date: Tue, 14 Aug 2018 13:27:13 -0500 Subject: [PATCH] updates (to StringSwap) --- Prototypes/String.wl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/Prototypes/String.wl b/Prototypes/String.wl index 54b3ffe..80cfae3 100644 --- a/Prototypes/String.wl +++ b/Prototypes/String.wl @@ -22,14 +22,19 @@ $UUIDStringPattern = CapitalizeSentences[string_] := StringReplace[string, Map[# -> Capitalize[#] &, TextSentences[string]]] +StringSwap::noswap = "Unable to swap, because \"`1`\" and \"`2`\" were found in overlapping positions in \"`3`\"."; + StringSwap[string_String, a_String <-> b_String] := Module[{i1,i2,ok}, + If[ Or[Not[StringContainsQ[string,a]],Not[StringContainsQ[string,b]]], Return[string]]; i1 = Interval /@ StringPosition[string,a]; i2 = Interval /@ StringPosition[string,b]; ok = SameQ[ {Interval[]}, Union @ Flatten @ Outer[IntervalIntersection,i1,i2]]; If[ ok, StringReplace[string, {a -> b, b -> a}], - $Failed (* swapping can be problematic with things like "ab" <-> "bc"; give up on those cases for now *) + Message[ StringSwap::noswap, a,b,string]; + string (* swapping can be problematic with things like "ab" <-> "bc"; give up on those cases for now *) ] +] StringComplement[args___String] := StringJoin[Complement @@ Map[Characters, {args}]]