Repository navigation
Expand file tree
/
Copy pathRule_ID.ml
More file actions
77 lines (63 loc) · 2.52 KB
/
Copy pathRule_ID.ml
File metadata and controls
77 lines (63 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(* Martin Jambon
*
* Copyright (C) 2023 Semgrep Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* version 2.1 as published by the Free Software Foundation, with the
* special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file
* LICENSE for more details.
*)
(*****************************************************************************)
(* Prelude *)
(*****************************************************************************)
(* Rule identifiers (rule IDs), e.g., "ocaml.lang.list-no-map".
*
* History: this used to be defined in Rule.ml as a simple type alias,
* but better to provide a precise API to manipulate Rule IDs.
TODO: distinguish the bare rule name 'foo' from the path 'path.to.foo'
*)
open Sexplib.Std
(*****************************************************************************)
(* Types *)
(*****************************************************************************)
type t = string [@@deriving show, eq, sexp]
exception Malformed_rule_ID of string
(*****************************************************************************)
(* Entry points *)
(*****************************************************************************)
let to_string x = x
let validate =
let rex = Pcre2_.compile_exn "^[a-zA-Z0-9._-]*$" in
fun str -> Pcre2_.is_match rex str |> Result.value ~default:false
let sanitize_string str =
let buf = Buffer.create (String.length str) in
String.iter
(function
| ('a' .. 'z' | 'A' .. 'Z' | '0' .. '9' | '.' | '_' | '-') as c ->
Buffer.add_char buf c
| _junk -> ())
str;
Buffer.contents buf
let of_string_exn x =
if not (validate x) then raise (Malformed_rule_ID x) else x
let of_string_opt x = if validate x then Some x else None
let to_string_list x = x
let of_string_list x = x
let compare = String.compare
let last_elt_opt x =
(* ocaml.best-practice.use-obj-magic
* -> ['ocaml';'best-practice';'use-obj-magic']
*)
let rule_name_seperated = String.split_on_char '.' x in
List_.last_opt rule_name_seperated
let ends_with r ~suffix:inc_or_exc_rule =
r = inc_or_exc_rule || String.ends_with ~suffix:("." ^ inc_or_exc_rule) r
(* For ATD 'string wrap' in semgrep_output_v1.atd *)
let unwrap = to_string
let wrap = of_string_exn
let dash_e = of_string_exn "-"