Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[enhance] stdlib/string: Adding has_suffix & has_prefix functions.

  • Loading branch information...
commit 069a44a67be6fc81c0cac79e0066ee0c262cef62 1 parent d3757ec
@akoprow akoprow authored
Showing with 23 additions and 0 deletions.
  1. +23 −0 stdlib/core/string.opa
View
23 stdlib/core/string.opa
@@ -571,7 +571,30 @@ String =
else source ^ "a"
: string
+ /**
+ * Returns true iff the source string has a given suffix.
+ *
+ * @param source a source string
+ * @param suffix a suffix to check
+ * @reutrn true iff [source] has suffix [suffix]
+ */
+ has_suffix(source: string, suffix: string) : bool =
+ i = length(suffix)
+ match substring_opt(String.length(source) - i, i, source)
+ | {none} -> false
+ | {some=source_suffix} -> equals(suffix, source_suffix)
+ /**
+ * Returns true iff the source string has a given prefix.
+ *
+ * @param source a source string
+ * @param prefix a prefix to check
+ * @reutrn true iff [source] has prefix [prefix]
+ */
+ has_prefix(source: string, prefix: string) : bool =
+ match substring_opt(0, String.length(prefix), source)
+ | {none} -> false
+ | {some=source_prefix} -> equals(prefix, source_prefix)
/**
Please sign in to comment.
Something went wrong with that request. Please try again.