[m-rev.] diff: Add string.det_remove_prefix/3

Paul Bone paul at bone.id.au
Fri Apr 8 11:41:10 AEST 2016


Add string.det_remove_prefix/3

The string library has det_remove_suffix/3, so I've added the corresponding
det_remove_prefix/3.

library/string.m:
    As above.

NEWS:
    Update news file.
---
 NEWS             |  1 +
 library/string.m | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/NEWS b/NEWS
index 5910514..71c8764 100644
--- a/NEWS
+++ b/NEWS
@@ -218,6 +218,7 @@ Changes to the Mercury standard library:
    - to_utf16_code_unit_list/2
    - from_utf8_code_unit_list/2
    - from_utf16_code_unit_list/2
+   - det_remove_prefix/3
 
 * The following predicates have been added to the map module:
 
diff --git a/library/string.m b/library/string.m
index 74e9c08..273bd7b 100644
--- a/library/string.m
+++ b/library/string.m
@@ -733,6 +733,13 @@
     %
 :- pred remove_prefix(string::in, string::in, string::out) is semidet.
 
+    % det_remove_prefix(Prefix, String, Suffix):
+    %
+    % This is a synonym for append(Prefix, Suffix, String) but with the
+    % arguments in a more convenient order for use with higher-order code.
+    %
+:- pred det_remove_prefix(string::in, string::in, string::out) is det.
+
     % remove_prefix_if_present(Prefix, String) = Suffix returns `String' minus
     % `Prefix' if `String' begins with `Prefix', and `String' if it doesn't.
     %
@@ -4506,6 +4513,13 @@ suffix_2_ioii(String, Suffix, Cur, Len) :-
 string.remove_prefix(Prefix, String, Suffix) :-
     string.append(Prefix, Suffix, String).
 
+det_remove_prefix(Prefix, String, Suffix) :-
+    ( if remove_prefix(Prefix, String, SuffixPrime) then
+        Suffix = SuffixPrime
+    else
+        error($pred, "string does not have the given prefix")
+    ).
+
 string.remove_prefix_if_present(Prefix, String) = Out :-
     ( if string.remove_prefix(Prefix, String, Suffix) then
         Out = Suffix
-- 
2.8.0.rc3



More information about the reviews mailing list