Skip to content

Commit

Permalink
Merge pull request #16 from copy/master
Browse files Browse the repository at this point in the history
Add decode_opt, an option-returning variant of decode
  • Loading branch information
djs55 authored Jul 15, 2018
2 parents c0d5881 + 72552e0 commit 6002bdd
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 6002bdd

Please sign in to comment.