(**
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the "hack" directory of this source tree.
 *
 *)

let initialized = ref false

let base64_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

let alphanumeric_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

let () =
  assert (String.length base64_alphabet = 64)

let short_string_with_alphabet alphabet =
  (* If we haven't seeded random then do it now *)
  if not !initialized then begin
    initialized := true;
    Random.self_init ()
  end;
  let r = ref ((Random.bits () lsl 30) lor Random.bits ()) in
  let cs = ref [] in
  while !r > 0 do
    let c = alphabet.[!r mod (String.length alphabet)] in
    cs := String.make 1 c :: !cs;
    r := !r lsr 6
  done;
  String.concat "" !cs

let short_string () =
  short_string_with_alphabet base64_alphabet

let short_alphanumeric_string () =
  short_string_with_alphabet alphanumeric_alphabet

(**
 * Copyright (c) 2015, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the "hack" directory of this source tree.
 *
 *)

let initialized = ref false

(* Do not use / in random ids as they appear in filenames. *)
let alphanumeric_alphabet =
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"

let short_string_with_alphabet alphabet =
  (* If we haven't seeded random then do it now *)
  if not !initialized then begin
    initialized := true;
    Random.self_init ()
  end;
  let r = ref ((Random.bits () lsl 30) lor Random.bits ()) in
  let cs = ref [] in
  while !r > 0 do
    let c = alphabet.[!r mod (String.length alphabet)] in
    cs := String.make 1 c :: !cs;
    r := !r lsr 6
  done;
  String.concat "" !cs

let short_string () =
  short_string_with_alphabet alphanumeric_alphabet

Fix random id bug when it contains slashes
[hack] Fix random id bug when it contains slashes

Summary:
Hack uses random ids in many places. Some of them are used for filenames, but turn out to have `/`es in them which causes random occasional failures.

Gets rid of random ids with `/`

Impact on Hack is that the random ids have reduced entropy. One way we could address this is to add 2 characters that are valid in filenames.

Reviewed By: ljw1004

Differential Revision: D13309389

fbshipit-source-id: 9c01c7cdaeeae5f83e938b47422046076521726c
