Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decode_opt, an option-returning variant of decode #16

Merged
merged 1 commit into from
Jul 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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