Skip to content

Commit

Permalink
Switch to Re for pcre. (#2085)
Browse files Browse the repository at this point in the history
  • Loading branch information
toots authored Nov 10, 2023
1 parent a1b23fd commit 4cfb499
Show file tree
Hide file tree
Showing 48 changed files with 259 additions and 371 deletions.
2 changes: 1 addition & 1 deletion .github/opam/liquidsoap-core-windows.opam
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ depends: [
"dtools-windows" {>= "0.4.5"}
"duppy-windows" {>= "0.9.3"}
"mm-windows" {>= "0.8.4"}
"pcre-windows" {>= "7.5.0"}
"re-windows" {>= "1.11.0"}
"cry-windows" {>= "1.0.1"}
"sedlex" {>= "3.2"}
"sedlex-windows" {>= "3.2"}
Expand Down
3 changes: 3 additions & 0 deletions .github/scripts/build-posix.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ sed -e 's@ocaml-gstreamer@#ocaml-gstreamer@' -i PACKAGES

export PKG_CONFIG_PATH=/usr/share/pkgconfig/pkgconfig

opam update
opam install -y re.1.11.0

echo "::endgroup::"

echo "::group::Checking out CI commit"
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ jobs:
run: |
cd /tmp/liquidsoap-full/liquidsoap
eval "$(opam config env)"
opam update
opam install -y re.1.11.0
dune build --profile release ./src/js/interactive_js.bc.js
tree_sitter_parse:
Expand Down
3 changes: 2 additions & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
(dtools (>= 0.4.5))
(duppy (>= 0.9.3))
(mm (>= 0.8.4))
(pcre (>= 7.5.0))
(re (>= 1.11.0))
(ocurl (>= 0.9.2))
(cry (>= 1.0.0))
(camomile (>= 2.0.0))
Expand Down Expand Up @@ -151,6 +151,7 @@
(depends
(ocaml (>= 4.14.0))
dune-site
(re (>= 1.11.0))
(ppx_string :build)
(sedlex (>= 3.2))
(menhir (>= 20180703)))
Expand Down
2 changes: 1 addition & 1 deletion liquidsoap-core.opam
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ depends: [
"dtools" {>= "0.4.5"}
"duppy" {>= "0.9.3"}
"mm" {>= "0.8.4"}
"pcre" {>= "7.5.0"}
"re" {>= "1.11.0"}
"ocurl" {>= "0.9.2"}
"cry" {>= "1.0.0"}
"camomile" {>= "2.0.0"}
Expand Down
1 change: 1 addition & 0 deletions liquidsoap-lang.opam
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ depends: [
"dune" {>= "3.6"}
"ocaml" {>= "4.14.0"}
"dune-site"
"re" {>= "1.11.0"}
"ppx_string" {build}
"sedlex" {>= "3.2"}
"menhir" {>= "20180703"}
Expand Down
2 changes: 2 additions & 0 deletions src/core/builtins/builtins_files.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module Pcre = Re.Pcre

module Filename = struct
include Filename

Expand Down
4 changes: 3 additions & 1 deletion src/core/builtins/builtins_server.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*****************************************************************************)

module Pcre = Re.Pcre

let _ =
Lang.add_builtin ~base:Modules.server "register" ~category:`Interaction
~descr:
Expand Down Expand Up @@ -64,6 +66,6 @@ let _ =
in
let f = Lang.assoc "" 2 p in
let f x = Lang.to_string (Lang.apply f [("", Lang.string x)]) in
let ns = Pcre.split ~pat:"\\." namespace in
let ns = Pcre.split ~rex:(Pcre.regexp "\\.") namespace in
Server.add ~ns ~usage ~descr command f;
Lang.unit)
3 changes: 2 additions & 1 deletion src/core/builtins/builtins_sys.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*****************************************************************************)

open Extralib
module Pcre = Re.Pcre

let configure = Modules.configure

Expand Down Expand Up @@ -224,7 +225,7 @@ let _ =
let a = Lang.to_string (Lang.assoc "" 2 p) in
let s = match a with "" -> c | _ -> c ^ " " ^ a in
let r = try Server.exec s with Not_found -> "Command not found!" in
Lang.list (List.map Lang.string (Pcre.split ~pat:"\r?\n" r))
Lang.list (List.map Lang.string (Pcre.split ~rex:(Pcre.regexp "\r?\n") r))
in
Lang.add_builtin ~base:Modules.server "execute" ~category ~descr params
return_t execute
Expand Down
1 change: 1 addition & 0 deletions src/core/dune
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
camomile.lib
curl
cry
re
uri
metadata
(select
Expand Down
8 changes: 6 additions & 2 deletions src/core/encoder/formats/ffmpeg_format.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*****************************************************************************)

module Pcre = Re.Pcre

type opt_val =
[ `String of string | `Int of int | `Int64 of int64 | `Float of float ]

Expand Down Expand Up @@ -135,7 +137,8 @@ let to_string m =
| None -> `Var "none"
| Some d -> `String d);
Printf.sprintf "%%%s(%s%s)" name
(if Pcre.pmatch ~pat:"video" name then "" else "video_content,")
(if Pcre.pmatch ~rex:(Pcre.regexp "video") name then ""
else "video_content,")
(string_of_options stream_opts)
:: opts
| `Encode { codec; options = `Audio options; opts = stream_opts } ->
Expand All @@ -149,7 +152,8 @@ let to_string m =
Hashtbl.add stream_opts "samplerate"
(`Int (Lazy.force options.samplerate));
Printf.sprintf "%s(%s%s)" name
(if Pcre.pmatch ~pat:"audio" name then "" else "audio_content,")
(if Pcre.pmatch ~rex:(Pcre.regexp "audio") name then ""
else "audio_content,")
(string_of_options stream_opts)
:: opts)
opts m.streams
Expand Down
27 changes: 20 additions & 7 deletions src/core/harbor/harbor.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
*****************************************************************************)

open Harbor_base
module Pcre = Re.Pcre
module Monad = Duppy.Monad
module Type = Liquidsoap_lang.Type
module Regexp = Liquidsoap_lang.Regexp
module Http_base = Http

let ( let* ) = Duppy.Monad.bind
Expand Down Expand Up @@ -362,7 +362,7 @@ module Make (T : Transport_t) : T with type socket = T.socket = struct
let websocket_error n msg = Websocket.to_string (`Close (Some (n, msg)))

let parse_icy_request_line ~port h r =
let auth_data = Pcre.split ~pat:":" r in
let auth_data = Pcre.split ~rex:(Pcre.regexp ":") r in
let requested_user, password =
match auth_data with
| user :: password :: _ -> (user, password)
Expand Down Expand Up @@ -453,7 +453,9 @@ module Make (T : Transport_t) : T with type socket = T.socket = struct
let data = Pcre.split ~rex:(Pcre.regexp "[ \t]+") auth in
match data with
| "Basic" :: x :: _ -> (
let auth_data = Pcre.split ~pat:":" (Lang_string.decode64 x) in
let auth_data =
Pcre.split ~rex:(Pcre.regexp ":") (Lang_string.decode64 x)
in
match auth_data with
| x :: y :: _ -> (x, y)
| _ -> raise Not_found)
Expand Down Expand Up @@ -788,11 +790,22 @@ module Make (T : Transport_t) : T with type socket = T.socket = struct

(* First, try with a registered handler. *)
let { handler; _ } = find_handler port in
let f (verb, rex, handler) =
if (verb :> verb) = hmethod && Lang.Regexp.test rex base_uri then (
let { Lang.Regexp.groups } = Lang.Regexp.exec rex base_uri in
let f (verb, regex, handler) =
let rex = regex.Liquidsoap_lang.Builtins_regexp.regexp in
let sub = lazy (try Some (Re.Pcre.exec ~rex base_uri) with _ -> None) in
if (verb :> verb) = hmethod && Lazy.force sub <> None then (
let sub = Option.get (Lazy.force sub) in
let groups =
List.fold_left
(fun groups name ->
try (name, Re.Pcre.get_named_substring rex name sub) :: groups
with Not_found -> groups)
[]
(Array.to_list (Re.Pcre.names rex))
in
log#info "Found handler '%s %s' on port %d%s." smethod
(Lang.descr_of_regexp rex) port
(Lang.descr_of_regexp regex)
port
(match groups with
| [] -> ""
| groups ->
Expand Down
40 changes: 0 additions & 40 deletions src/core/hooks_implementations.ml
Original file line number Diff line number Diff line change
@@ -1,45 +1,6 @@
module Hooks = Liquidsoap_lang.Hooks
module Lang = Liquidsoap_lang.Lang

let cflags_of_flags (flags : Liquidsoap_lang.Regexp.flag list) =
List.fold_left
(fun l f ->
match f with
| `i -> `CASELESS :: l
(* `g is handled at the call level. *)
| `g -> l
| `s -> `DOTALL :: l
| `m -> `MULTILINE :: l)
[] flags

let regexp ?(flags = []) s =
let iflags = Pcre.cflags (cflags_of_flags flags) in
let rex = Pcre.regexp ~iflags s in
object
method split s = Pcre.split ~rex s

method exec s =
let sub = Pcre.exec ~rex s in
let matches = Array.to_list (Pcre.get_opt_substrings sub) in
let groups =
List.fold_left
(fun groups name ->
try (name, Pcre.get_named_substring rex name sub) :: groups
with _ -> groups)
[]
(Array.to_list (Pcre.names rex))
in
{ Lang.Regexp.matches; groups }

method test s = Pcre.pmatch ~rex s

method substitute ~subst s =
let substitute =
if List.mem `g flags then Pcre.substitute else Pcre.substitute_first
in
substitute ~rex ~subst s
end

(* For source eval check there are cases of:
source('a) <: (source('a).{ source methods })?
b/c of source.dynamic so we want to dig deeper
Expand Down Expand Up @@ -153,7 +114,6 @@ let register () =
Dtools.Log.conf_file#on_change on_change;
ignore (Option.map on_change Dtools.Log.conf_file#get_d);
Hooks.collect_after := Clock.collect_after;
Hooks.regexp := regexp;
(Hooks.make_log := fun name -> (Log.make name :> Hooks.log));
Hooks.type_of_encoder := Lang_encoder.type_of_encoder;
Hooks.make_encoder := Lang_encoder.make_encoder;
Expand Down
11 changes: 1 addition & 10 deletions src/core/lang.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type t = Liquidsoap_lang.Type.t

type module_name = Liquidsoap_lang.Lang.module_name
type scheme = Liquidsoap_lang.Type.scheme
type regexp
type regexp = Liquidsoap_lang.Lang.regexp

(** {2 Values} *)

Expand Down Expand Up @@ -333,12 +333,3 @@ val descr_of_regexp : regexp -> string

(** Return a string description of a regexp value i.e. r/^foo\/bla$/g *)
val string_of_regexp : regexp -> string

module Regexp : sig
include Liquidsoap_lang.Regexp.T with type t := regexp

type sub = Liquidsoap_lang.Regexp.sub = {
matches : string option list;
groups : (string * string) list;
}
end
5 changes: 4 additions & 1 deletion src/core/operators/chord.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

open Mm
open Source
module Pcre = Re.Pcre

let chan = 0

Expand Down Expand Up @@ -71,7 +72,9 @@ class chord metadata_name (source : source) =
| Some c -> (
try
let sub =
Pcre.exec ~pat:"^([A-G-](?:b|#)?)(|M|m|M7|m7|dim)$" c
Pcre.exec
~rex:(Pcre.regexp "^([A-G-](?:b|#)?)(|M|m|M7|m7|dim)$")
c
in
let n = Pcre.get_substring sub 1 in
let n = note_of_string n in
Expand Down
6 changes: 5 additions & 1 deletion src/core/outputs/harbor_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ class output p =
let uri =
match mount.[0] with '/' -> mount | _ -> Printf.sprintf "%c%s" '/' mount
in
let uri = Lang.Regexp.regexp [%string {|^%{uri}$|}] in
let uri =
let regexp = [%string {|^%{uri}$|}] in
Liquidsoap_lang.Builtins_regexp.
{ descr = regexp; flags = []; regexp = Re.Pcre.regexp regexp }
in
let autostart = Lang.to_bool (List.assoc "start" p) in
let infallible = not (Lang.to_bool (List.assoc "fallible" p)) in
let on_start =
Expand Down
6 changes: 5 additions & 1 deletion src/core/outputs/pipe_output.ml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

(** base class *)

module Pcre = Re.Pcre

let output = Modules.output

let encoder_factory ?format format_val =
Expand Down Expand Up @@ -436,7 +438,9 @@ class virtual ['a] file_output_base p =
let filename = filename () in
let filename = Lang_string.home_unrelate filename in
(* Avoid / in metas for filename.. *)
let subst m = Pcre.substitute ~pat:"/" ~subst:(fun _ -> "-") m in
let subst m =
Pcre.substitute ~rex:(Pcre.regexp "/") ~subst:(fun _ -> "-") m
in
self#interpolate ~subst filename
method virtual open_out_gen : open_flag list -> int -> string -> 'a
Expand Down
21 changes: 15 additions & 6 deletions src/core/playlists/playlist_basic.ml
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@
*****************************************************************************)

module Pcre = Re.Pcre

let log = Log.make ["playlist"; "basic"]
let split_lines buf = Pcre.split ~pat:"[\r\n]+" buf
let split_lines buf = Pcre.split ~rex:(Pcre.regexp "[\r\n]+") buf

let parse_meta =
let processor =
Expand Down Expand Up @@ -61,7 +63,7 @@ let parse_extinf s =
| "" -> meta
| duration -> ("extinf_duration", duration) :: meta
in
let lines = Pcre.split ~pat:"\\s*-\\s*" song in
let lines = Pcre.split ~rex:(Pcre.regexp "\\s*-\\s*") song in
meta
@
match lines with
Expand All @@ -75,7 +77,7 @@ let parse_extinf s =
(* This parser cannot detect the format !! *)
let parse_mpegurl ?pwd string =
let lines = List.filter (fun x -> x <> "") (split_lines string) in
let is_info line = Pcre.pmatch ~pat:"^#EXTINF" line in
let is_info line = Pcre.pmatch ~rex:(Pcre.regexp "^#EXTINF") line in
let skip_line line = line.[0] == '#' in
let rec get_urls cur lines =
match lines with
Expand All @@ -90,10 +92,16 @@ let parse_mpegurl ?pwd string =
get_urls [] lines

let parse_scpls ?pwd string =
let string = Pcre.replace ~pat:"#[^\\r\\n]*[\\n\\r]+" string in
let string =
Pcre.substitute
~rex:(Pcre.regexp "#[^\\r\\n]*[\\n\\r]+")
~subst:(fun _ -> "")
string
in
(* Format check, raise Not_found if invalid *)
ignore
(Pcre.exec ~pat:"^[\\r\\n\\s]*\\[playlist\\]"
(Pcre.exec
~rex:(Pcre.regexp "^[\\r\\n\\s]*\\[playlist\\]")
(String.lowercase_ascii string));
let lines = split_lines string in
let urls =
Expand Down Expand Up @@ -226,7 +234,8 @@ let parse_cue ?pwd string =
let strings = split_lines string in
let strings =
List.map
(fun string -> Pcre.replace ~rex:(Pcre.regexp "^\\s+") string)
(fun string ->
Pcre.substitute ~rex:(Pcre.regexp "^\\s+") ~subst:(fun _ -> "") string)
strings
in
let strings = List.filter (fun s -> s <> "") strings in
Expand Down
Loading

0 comments on commit 4cfb499

Please sign in to comment.