Skip to content

Commit 47a5d3b

Browse files
committed
add Ext_filename.new_extension API
1 parent 0e383fb commit 47a5d3b

10 files changed

+365
-19
lines changed

Diff for: jscomp/bsb_helper/bsb_helper_depfile_gen.ml

+2-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,8 @@ let emit_d mlast
231231

232232
let buf = Ext_buffer.create 2048 in
233233
let input_file = Filename.chop_extension mlast in
234-
let filename = input_file ^ Literals.suffix_d in
234+
let filename =
235+
Ext_filename.new_extension mlast Literals.suffix_d in
235236
let lhs_suffix = Literals.suffix_cmj in
236237
let rhs_suffix = Literals.suffix_cmj in
237238
oc_impl

Diff for: jscomp/ext/ext_filename.ml

+23-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@
2525

2626

2727

28+
let is_dir_sep_unix c = c = '/'
29+
let is_dir_sep_win_cygwin c =
30+
c = '/' || c = '\\' || c = ':'
2831

32+
let is_dir_sep =
33+
if Sys.unix then is_dir_sep_unix else is_dir_sep_win_cygwin
2934

3035
(* reference ninja.cc IsKnownShellSafeCharacter *)
3136
let maybe_quote ( s : string) =
@@ -43,10 +48,23 @@ let maybe_quote ( s : string) =
4348
s
4449
else Filename.quote s
4550

46-
(*
47-
let new_extension name =
51+
52+
let chop_extension_maybe name =
4853
let rec search_dot i =
49-
if i < 0 || Filename.is_dir_sep name i then invalid_arg "Filename.chop_extension"
50-
else if name.[i] = '.' then String.sub name 0 i
54+
if i < 0 || is_dir_sep (String.unsafe_get name i) then name
55+
else if String.unsafe_get name i = '.' then String.sub name 0 i
5156
else search_dot (i - 1) in
52-
search_dot (String.length name - 1) *)
57+
search_dot (String.length name - 1)
58+
59+
60+
let new_extension name (ext : string) =
61+
let rec search_dot name i ext =
62+
if i < 0 || is_dir_sep (String.unsafe_get name i) then name
63+
else if String.unsafe_get name i = '.' then
64+
let ext_len = String.length ext in
65+
let buf = Bytes.create (i + ext_len) in
66+
Bytes.blit_string name 0 buf 0 i;
67+
Bytes.blit_string ext 0 buf i ext_len;
68+
Bytes.unsafe_to_string buf
69+
else search_dot name (i - 1) ext in
70+
search_dot name (String.length name - 1) ext

Diff for: jscomp/ext/ext_filename.mli

+9
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,14 @@
4141

4242

4343
val maybe_quote:
44+
string ->
45+
string
46+
47+
val chop_extension_maybe:
48+
stirng ->
49+
string
50+
51+
val new_extension:
52+
string ->
4453
string ->
4554
string

Diff for: jscomp/ounit_tests/ounit_string_tests.ml

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let (=~) = OUnit.assert_equal
55

66
let printer_string = fun x -> x
77

8+
let string_eq = OUnit.assert_equal ~printer:(fun id -> id)
89

910
let suites =
1011
__FILE__ >:::
@@ -453,5 +454,12 @@ let suites =
453454
print_endline bench; *)
454455
OUnit.assert_bool
455456
__LOC__ (not (Ext_buffer.not_equal buf bench))
456-
end
457+
end ;
458+
459+
__LOC__ >:: begin fun _ ->
460+
string_eq (Ext_filename.new_extension "a.c" ".xx") "a.xx";
461+
string_eq (Ext_filename.new_extension "abb.c" ".xx") "abb.xx"
462+
end;
457463
]
464+
465+
;; string_eq (Ext_filename.new_extension "abb.c" ".xx") "abb.xx" |> ignore

Diff for: lib/4.02.3/bsb.ml

+28-4
Original file line numberDiff line numberDiff line change
@@ -14143,6 +14143,12 @@ module Ext_filename : sig
1414314143
val maybe_quote:
1414414144
string ->
1414514145
string
14146+
14147+
14148+
val new_extension:
14149+
string ->
14150+
string ->
14151+
string
1414614152
end = struct
1414714153
#1 "ext_filename.ml"
1414814154
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
@@ -14172,7 +14178,12 @@ end = struct
1417214178

1417314179

1417414180

14181+
let is_dir_sep_unix c = c = '/'
14182+
let is_dir_sep_win_cygwin c =
14183+
c = '/' || c = '\\' || c = ':'
1417514184

14185+
let is_dir_sep =
14186+
if Sys.unix then is_dir_sep_unix else is_dir_sep_win_cygwin
1417614187

1417714188
(* reference ninja.cc IsKnownShellSafeCharacter *)
1417814189
let maybe_quote ( s : string) =
@@ -14190,13 +14201,26 @@ let maybe_quote ( s : string) =
1419014201
s
1419114202
else Filename.quote s
1419214203

14193-
(*
14194-
let new_extension name =
14204+
14205+
let chop_extension_maybe name =
1419514206
let rec search_dot i =
14196-
if i < 0 || Filename.is_dir_sep name i then invalid_arg "Filename.chop_extension"
14207+
if i < 0 || is_dir_sep name.[i] then name
1419714208
else if name.[i] = '.' then String.sub name 0 i
1419814209
else search_dot (i - 1) in
14199-
search_dot (String.length name - 1) *)
14210+
search_dot (String.length name - 1)
14211+
14212+
14213+
let new_extension name (ext : string) =
14214+
let rec search_dot name i ext =
14215+
if i < 0 || is_dir_sep name.[i] then name
14216+
else if name.[i] = '.' then
14217+
let ext_len = String.length ext in
14218+
let buf = Bytes.create (i + ext_len) in
14219+
Bytes.blit_string name 0 buf 0 i;
14220+
Bytes.blit_string ext 0 buf i ext_len;
14221+
Bytes.unsafe_to_string buf
14222+
else search_dot name (i - 1) ext in
14223+
search_dot name (String.length name - 1) ext
1420014224

1420114225
end
1420214226
module Bsb_ninja_gen : sig

Diff for: lib/4.02.3/bsb_helper.ml

+129-1
Original file line numberDiff line numberDiff line change
@@ -4237,6 +4237,133 @@ let not_equal (b : t) (s : string) =
42374237
|| not_equal_aux b.buffer s 0 s_len
42384238

42394239

4240+
end
4241+
module Ext_filename : sig
4242+
#1 "ext_filename.mli"
4243+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
4244+
*
4245+
* This program is free software: you can redistribute it and/or modify
4246+
* it under the terms of the GNU Lesser General Public License as published by
4247+
* the Free Software Foundation, either version 3 of the License, or
4248+
* (at your option) any later version.
4249+
*
4250+
* In addition to the permissions granted to you by the LGPL, you may combine
4251+
* or link a "work that uses the Library" with a publicly distributed version
4252+
* of this file to produce a combined library or application, then distribute
4253+
* that combined work under the terms of your choosing, with no requirement
4254+
* to comply with the obligations normally placed on you by section 4 of the
4255+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
4256+
* should you choose to use a later version).
4257+
*
4258+
* This program is distributed in the hope that it will be useful,
4259+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
4260+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4261+
* GNU Lesser General Public License for more details.
4262+
*
4263+
* You should have received a copy of the GNU Lesser General Public License
4264+
* along with this program; if not, write to the Free Software
4265+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
4266+
4267+
4268+
4269+
4270+
4271+
(* TODO:
4272+
Change the module name, this code is not really an extension of the standard
4273+
library but rather specific to JS Module name convention.
4274+
*)
4275+
4276+
4277+
4278+
4279+
4280+
(** An extension module to calculate relative path follow node/npm style.
4281+
TODO : this short name will have to change upon renaming the file.
4282+
*)
4283+
4284+
4285+
val maybe_quote:
4286+
string ->
4287+
string
4288+
4289+
4290+
val new_extension:
4291+
string ->
4292+
string ->
4293+
string
4294+
end = struct
4295+
#1 "ext_filename.ml"
4296+
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
4297+
*
4298+
* This program is free software: you can redistribute it and/or modify
4299+
* it under the terms of the GNU Lesser General Public License as published by
4300+
* the Free Software Foundation, either version 3 of the License, or
4301+
* (at your option) any later version.
4302+
*
4303+
* In addition to the permissions granted to you by the LGPL, you may combine
4304+
* or link a "work that uses the Library" with a publicly distributed version
4305+
* of this file to produce a combined library or application, then distribute
4306+
* that combined work under the terms of your choosing, with no requirement
4307+
* to comply with the obligations normally placed on you by section 4 of the
4308+
* LGPL version 3 (or the corresponding section of a later version of the LGPL
4309+
* should you choose to use a later version).
4310+
*
4311+
* This program is distributed in the hope that it will be useful,
4312+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
4313+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4314+
* GNU Lesser General Public License for more details.
4315+
*
4316+
* You should have received a copy of the GNU Lesser General Public License
4317+
* along with this program; if not, write to the Free Software
4318+
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
4319+
4320+
4321+
4322+
4323+
let is_dir_sep_unix c = c = '/'
4324+
let is_dir_sep_win_cygwin c =
4325+
c = '/' || c = '\\' || c = ':'
4326+
4327+
let is_dir_sep =
4328+
if Sys.unix then is_dir_sep_unix else is_dir_sep_win_cygwin
4329+
4330+
(* reference ninja.cc IsKnownShellSafeCharacter *)
4331+
let maybe_quote ( s : string) =
4332+
let noneed_quote =
4333+
Ext_string.for_all s (function
4334+
| '0' .. '9'
4335+
| 'a' .. 'z'
4336+
| 'A' .. 'Z'
4337+
| '_' | '+'
4338+
| '-' | '.'
4339+
| '/' -> true
4340+
| _ -> false
4341+
) in
4342+
if noneed_quote then
4343+
s
4344+
else Filename.quote s
4345+
4346+
4347+
let chop_extension_maybe name =
4348+
let rec search_dot i =
4349+
if i < 0 || is_dir_sep name.[i] then name
4350+
else if name.[i] = '.' then String.sub name 0 i
4351+
else search_dot (i - 1) in
4352+
search_dot (String.length name - 1)
4353+
4354+
4355+
let new_extension name (ext : string) =
4356+
let rec search_dot name i ext =
4357+
if i < 0 || is_dir_sep name.[i] then name
4358+
else if name.[i] = '.' then
4359+
let ext_len = String.length ext in
4360+
let buf = Bytes.create (i + ext_len) in
4361+
Bytes.blit_string name 0 buf 0 i;
4362+
Bytes.blit_string ext 0 buf i ext_len;
4363+
Bytes.unsafe_to_string buf
4364+
else search_dot name (i - 1) ext in
4365+
search_dot name (String.length name - 1) ext
4366+
42404367
end
42414368
module Ext_namespace : sig
42424369
#1 "ext_namespace.mli"
@@ -4806,7 +4933,8 @@ let emit_d mlast
48064933

48074934
let buf = Ext_buffer.create 2048 in
48084935
let input_file = Filename.chop_extension mlast in
4809-
let filename = input_file ^ Literals.suffix_d in
4936+
let filename =
4937+
Ext_filename.new_extension mlast Literals.suffix_d in
48104938
let lhs_suffix = Literals.suffix_cmj in
48114939
let rhs_suffix = Literals.suffix_cmj in
48124940
oc_impl

Diff for: lib/4.02.3/bsb_helper.ml.d

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
../lib/4.02.3/bsb_helper.ml: ./bsb_helper/bsb_db_decode.ml ./bsb_helper/bsb_db_decode.mli ./bsb_helper/bsb_helper_depfile_gen.ml ./bsb_helper/bsb_helper_depfile_gen.mli ./common/bs_version.ml ./common/bs_version.mli ./ext/bsb_db.ml ./ext/bsb_db.mli ./ext/bsb_dir_index.ml ./ext/bsb_dir_index.mli ./ext/ext_array.ml ./ext/ext_array.mli ./ext/ext_buffer.ml ./ext/ext_buffer.mli ./ext/ext_bytes.ml ./ext/ext_bytes.mli ./ext/ext_char.ml ./ext/ext_char.mli ./ext/ext_list.ml ./ext/ext_list.mli ./ext/ext_namespace.ml ./ext/ext_namespace.mli ./ext/ext_option.ml ./ext/ext_option.mli ./ext/ext_pervasives.ml ./ext/ext_pervasives.mli ./ext/ext_string.ml ./ext/ext_string.mli ./ext/literals.ml ./ext/literals.mli ./ext/map_gen.ml ./ext/string_map.ml ./ext/string_map.mli ./main/bsb_helper_main.ml ./main/bsb_helper_main.mli
1+
../lib/4.02.3/bsb_helper.ml: ./bsb_helper/bsb_db_decode.ml ./bsb_helper/bsb_db_decode.mli ./bsb_helper/bsb_helper_depfile_gen.ml ./bsb_helper/bsb_helper_depfile_gen.mli ./common/bs_version.ml ./common/bs_version.mli ./ext/bsb_db.ml ./ext/bsb_db.mli ./ext/bsb_dir_index.ml ./ext/bsb_dir_index.mli ./ext/ext_array.ml ./ext/ext_array.mli ./ext/ext_buffer.ml ./ext/ext_buffer.mli ./ext/ext_bytes.ml ./ext/ext_bytes.mli ./ext/ext_char.ml ./ext/ext_char.mli ./ext/ext_filename.ml ./ext/ext_filename.mli ./ext/ext_list.ml ./ext/ext_list.mli ./ext/ext_namespace.ml ./ext/ext_namespace.mli ./ext/ext_option.ml ./ext/ext_option.mli ./ext/ext_pervasives.ml ./ext/ext_pervasives.mli ./ext/ext_string.ml ./ext/ext_string.mli ./ext/literals.ml ./ext/literals.mli ./ext/map_gen.ml ./ext/string_map.ml ./ext/string_map.mli ./main/bsb_helper_main.ml ./main/bsb_helper_main.mli

0 commit comments

Comments
 (0)