Skip to content
This repository
Browse code

[enhance] stdlib/string: adding get_prefix & get_suffix functions.

  • Loading branch information...
commit 31162319afd635b6e04debac1e1adc4e103e96ef 1 parent 83d2b93
Adam Koprowski authored July 12, 2011

Showing 1 changed file with 34 additions and 14 deletions. Show diff stats Hide diff stats

  1. 48  stdlib/core/string.opa
48  stdlib/core/string.opa
@@ -193,6 +193,28 @@ String =
193 193
   : string
194 194
 
195 195
   /**
  196
+   * Gets a prefix of a given string of given length.
  197
+   *
  198
+   * @param length prefix length
  199
+   * @param source source string
  200
+   * @return [length]-character length prefix of [source] or [none] if
  201
+   *         [source] is shorter than [length] characters.
  202
+   */
  203
+  get_prefix(length: int, source: string) : option(string) =
  204
+    String.substring_opt(0, length, source)
  205
+
  206
+  /**
  207
+   * Gets a suffix of a given string of given length.
  208
+   *
  209
+   * @param length suffix length
  210
+   * @param source source string
  211
+   * @return [length]-character length suffix of [source] or [none] if
  212
+   *         [source] is shorter than [length] characters.
  213
+   */
  214
+  get_suffix(length: int, source: string) : option(string) =
  215
+    String.substring_opt(String.length(source) - length, length, source)
  216
+
  217
+  /**
196 218
    * [replace the first character by his uppercase]
197 219
    *
198 220
    * eg: [capitalize("toTo")] will return ["ToTo"]
@@ -572,30 +594,28 @@ String =
572 594
   : string
573 595
 
574 596
   /**
575  
-   * Returns true iff the source string has a given suffix.
  597
+   * Returns true iff the source string has a given prefix.
576 598
    *
  599
+   * @param prefix a prefix to check
577 600
    * @param source a source string
578  
-   * @param suffix a suffix to check
579  
-   * @reutrn true iff [source] has suffix [suffix]
  601
+   * @reutrn true iff [source] has prefix [prefix]
580 602
    */
581  
-  has_suffix(source: string, suffix: string) : bool =
582  
-    i = length(suffix)
583  
-    match substring_opt(String.length(source) - i, i, source)
  603
+  has_prefix(prefix: string, source: string) : bool =
  604
+    match get_prefix(length(prefix), source)
584 605
     | {none} -> false
585  
-    | {some=source_suffix} -> equals(suffix, source_suffix)
  606
+    | {some=source_prefix} -> equals(prefix, source_prefix)
586 607
 
587 608
   /**
588  
-   * Returns true iff the source string has a given prefix.
  609
+   * Returns true iff the source string has a given suffix.
589 610
    *
  611
+   * @param suffix a suffix to check
590 612
    * @param source a source string
591  
-   * @param prefix a prefix to check
592  
-   * @reutrn true iff [source] has prefix [prefix]
  613
+   * @reutrn true iff [source] has suffix [suffix]
593 614
    */
594  
-  has_prefix(source: string, prefix: string) : bool =
595  
-    match substring_opt(0, String.length(prefix), source)
  615
+  has_suffix(suffix: string, source: string) : bool =
  616
+    match get_suffix(length(suffix), source)
596 617
     | {none} -> false
597  
-    | {some=source_prefix} -> equals(prefix, source_prefix)
598  
-
  618
+    | {some=source_suffix} -> equals(suffix, source_suffix)
599 619
 
600 620
   /**
601 621
    *  ALIAS / SHORTHAND OF PREEXISTING FUNCTIONS

0 notes on commit 3116231

Please sign in to comment.
Something went wrong with that request. Please try again.