Skip to content

Commit

Permalink
Add decode_opt, an option-returning variant of decode
Browse files Browse the repository at this point in the history
  • Loading branch information
copy committed Jul 14, 2018
1 parent c0d5881 commit 72552e0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/b64.ml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,12 @@ let decode ?alphabet input =
done;
Bytes.unsafe_to_string output

let decode_opt ?alphabet input =
try
Some (decode ?alphabet input)
with
Not_found -> None

let encode ?(pad=true) ?alphabet input =
let length = String.length input in
let words = (length + 2) / 3 in (* rounded up *)
Expand Down
3 changes: 3 additions & 0 deletions src/b64.mli
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ val uri_safe_alphabet : string
@raise Not_found if [s] is not a valid Base64 string. *)
val decode : ?alphabet:string -> string -> string

(** Same as [decode], but returns [None] instead of raising. *)
val decode_opt : ?alphabet:string -> string -> string option

(** [encode s] encodes the string [s] into base64. If [pad] is false,
no trailing padding is added.
[pad] defaults to [true], and [alphabet] to {!default_alphabet}. *)
Expand Down

0 comments on commit 72552e0

Please sign in to comment.