Skip to content

Commit e88d04f

Browse files
committed
Fall back to TLA+ definition if Java module override of
`SequencesExt!ReplaceFirstSubSeq` cannot load Apache Commons Lang3 StringUtils class. (Not done for `ReplaceAllSubSeqs` because the TLA+ def fails because it involves `DOMAIN str` that TLC doesn't handle) Related to Github PR #59 #59 [Refactor] Signed-off-by: Markus Alexander Kuppe <[email protected]>
1 parent e43de9c commit e88d04f

File tree

2 files changed

+54
-4
lines changed

2 files changed

+54
-4
lines changed

modules/tlc2/overrides/SequencesExt.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
import java.util.ArrayList;
3030

31-
import org.apache.commons.lang3.StringUtils;
32-
3331
import tla2sany.semantic.ExprOrOpArgNode;
3432
import tlc2.output.EC;
3533
import tlc2.tool.EvalControl;
@@ -357,7 +355,11 @@ public static Value replaceFirstSubSeq(final Tool tool, final ExprOrOpArgNode[]
357355

358356
if(ss.equals("")) { return new StringValue(sr+st); }
359357

360-
return new StringValue(StringUtils.replaceOnce(st, ss, sr));
358+
try {
359+
return new StringValue(org.apache.commons.lang3.StringUtils.replaceOnce(st, ss, sr));
360+
} catch (NoClassDefFoundError e) {
361+
return null; // Handle with pure TLA+ definition of operator.
362+
}
361363
}
362364

363365
return null; // Non-string functions handled by pure TLA+ definition of operator.
@@ -384,7 +386,7 @@ public static Value replaceAllSubSeq(final Tool tool, final ExprOrOpArgNode[] ar
384386
return new StringValue(result.toString());
385387
}
386388

387-
return new StringValue(StringUtils.replace(st, ss, sr));
389+
return new StringValue(org.apache.commons.lang3.StringUtils.replace(st, ss, sr));
388390
}
389391

390392
return null; // Non-string functions handled by pure TLA+ definition of operator.

tests/SequencesExtTests.tla

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,54 @@ ASSUME(ReplaceFirstSubSeq("\r", "%%", "Properly escape the %% char") = "Properly
291291
ASSUME ReplaceFirstSubSeq("\\\\", "\\", "Properly escape the \\quotes") = "Properly escape the \\\\quotes"
292292
ASSUME ReplaceFirstSubSeq("replaces", "%pattern%", "This %pattern% the pattern") = "This replaces the pattern"
293293

294+
ReplaceFirstSubSeqPure(r, s, t) ==
295+
(**************************************************************************)
296+
(* The sequence t with its subsequence s replaced by the sequence r *)
297+
(**************************************************************************)
298+
IF s \in SubSeqs(t)
299+
THEN ReplaceSubSeqAt(IndexFirstSubSeq(s, t), r, s, t)
300+
ELSE t
301+
302+
ASSUME ReplaceFirstSubSeqPure(<<>>,<<>>,<<>>) = <<>>
303+
ASSUME ReplaceFirstSubSeqPure(<<4>>,<<>>,<<>>) = <<4>>
304+
ASSUME ReplaceFirstSubSeqPure(<<4>>,<<4>>,<<>>) = <<>>
305+
ASSUME ReplaceFirstSubSeqPure(<<>>,<<>>,<<3,2,3,4>>) = <<3,2,3,4>>
306+
ASSUME ReplaceFirstSubSeqPure(<<4,4>>,<<3,2,3,4>>,<<3,2,3,4>>) = <<4,4>>
307+
ASSUME ReplaceFirstSubSeqPure(<<4,4>>,<<>>,<<3,2,3,4>>) = <<4,4,3,2,3,4>>
308+
309+
ASSUME ReplaceFirstSubSeqPure(<<4,4>>,<<4>>,<<3,2,3,4>>) = <<3,2,3,4,4>>
310+
ASSUME ReplaceFirstSubSeqPure(<<>>,<<4>>,<<3,2,3,4>>) = <<3,2,3>>
311+
ASSUME ReplaceFirstSubSeqPure(<<>>,<<4>>,<<3,2,3,4,4>>) = <<3,2,3,4>>
312+
ASSUME ReplaceFirstSubSeqPure(<<4,4>>,<<3>>,<<3,2,3,4>>) = <<4,4,2,3,4>>
313+
ASSUME ReplaceFirstSubSeqPure(<<4>>, <<1,2>>, <<1,2,1,2>>) = <<4,1,2>>
314+
ASSUME ReplaceFirstSubSeqPure(<<4,4>>, <<1,2>>, <<1,2,1,2>>) = <<4,4,1,2>>
315+
ASSUME ReplaceFirstSubSeqPure(<<4,4,4>>, <<1,2>>, <<1,2,1,2>>) = <<4,4,4,1,2>>
316+
317+
ASSUME ReplaceFirstSubSeqPure(<<1,2>>, <<1,2>>, <<1,2,2,1>>) = <<1,2,2,1>>
318+
ASSUME ReplaceFirstSubSeqPure(<<2,1>>, <<1,2>>, <<1,2,2,1>>) = <<2,1,2,1>>
319+
320+
ASSUME \A seq \in (BoundedSeq(1..5, 5) \ {<<>>}):
321+
/\ ReplaceFirstSubSeqPure(<<6>>, <<>>, seq) = <<6>> \o seq
322+
/\ ReplaceFirstSubSeqPure(<<6>>, <<Head(seq)>>, seq) = <<6>> \o Tail(seq)
323+
324+
ASSUME ReplaceFirstSubSeqPure("", "", "") = ""
325+
ASSUME ReplaceFirstSubSeqPure("a", "", "") = "a"
326+
ASSUME ReplaceFirstSubSeqPure("a", "b", "") = ""
327+
ASSUME ReplaceFirstSubSeqPure("a", "d", "abc") = "abc"
328+
ASSUME ReplaceFirstSubSeqPure("ddd", "ab", "abab") = "dddab"
329+
ASSUME ReplaceFirstSubSeqPure("ddd", "aa", "aaa") = "ddda"
330+
ASSUME ReplaceFirstSubSeqPure("ddd", "abab", "abab") = "ddd"
331+
332+
ASSUME(ReplaceFirstSubSeqPure("\\", "%%", "Properly escape the %% char") = "Properly escape the \\ char")
333+
ASSUME(ReplaceFirstSubSeqPure("\"", "%%", "Properly escape the %% char") = "Properly escape the \" char")
334+
ASSUME(ReplaceFirstSubSeqPure("\n", "%%", "Properly escape the %% char") = "Properly escape the \n char")
335+
ASSUME(ReplaceFirstSubSeqPure("\t", "%%", "Properly escape the %% char") = "Properly escape the \t char")
336+
ASSUME(ReplaceFirstSubSeqPure("\f", "%%", "Properly escape the %% char") = "Properly escape the \f char")
337+
ASSUME(ReplaceFirstSubSeqPure("\r", "%%", "Properly escape the %% char") = "Properly escape the \r char")
338+
339+
ASSUME ReplaceFirstSubSeqPure("\\\\", "\\", "Properly escape the \\quotes") = "Properly escape the \\\\quotes"
340+
ASSUME ReplaceFirstSubSeqPure("replaces", "%pattern%", "This %pattern% the pattern") = "This replaces the pattern"
341+
294342
-----------------------------------------------------------------------------
295343

296344
ASSUME AssertEq(ReplaceAllSubSeqs(<<4>>,<<1>>,<<>>), <<>>)

0 commit comments

Comments
 (0)