Commits

Sebastien Mondet  committed afc62d1

lib: add `BASIC_STRING.sub_exn`

  • Participants
  • Parent commits 2c7b282

Comments (0)

Files changed (1)

       [length] is 0, [sub] returns [Some empty] whichever the other
       parameters are. *)
 
+  val sub_exn: t -> index:int -> length:int -> t
+  (** Do like [sub] but throw an exception instead of returning [None] *)
+
   val compare_substring: t * int * int -> t * int * int -> int
   (** Comparison function for substrings: use as [compare_substring
       (s1, index1, length1) (s2, index2, length2)].
     done;
     !res
 
+  let sub_exn t ~index ~length =
+    if length = 0 then empty else String.sub t index length
+
   let sub t ~index ~length =
     if length = 0 then Some empty else
       try Some (String.sub t index length)
       if !c = length then Some (List.rev !r) else None
     with
     | Not_found -> Some (List.rev !r)
+    
+  let sub_exn t ~index ~length =
+    match sub t ~index ~length with
+    | Some s -> s
+    | None -> ksprintf failwith "sub_exn(%d,%d)" index length
 
   let index_of_character t ?(from=0) c =
     let index = ref 0 in
         end
       end
 
+  let sub_exn t ~index ~length =
+    match sub t ~index ~length with
+    | Some s -> s
+    | None -> ksprintf failwith "sub_exn(%d,%d)" index length
+
   let to_string_hum t = to_native_string t |> sprintf "%S"
 
   let index_of_character t ?(from=0) c =