Skip to content

Commit

Permalink
[enhance] stdlib/string: Adding has_suffix & has_prefix functions.
Browse files Browse the repository at this point in the history
  • Loading branch information
akoprow committed Jul 12, 2011
1 parent d3757ec commit 069a44a
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions stdlib/core/string.opa
Expand Up @@ -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)


/**
Expand Down

0 comments on commit 069a44a

Please sign in to comment.