Skip to content

Commit ac6b638

Browse files
committed
make unsafe function names consistent
Fixes rescript-lang/rescript-core#225
1 parent 2a358eb commit ac6b638

File tree

3 files changed

+133
-1
lines changed

3 files changed

+133
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
- Deprecate JSON.Classify.classify. https://github.com/rescript-lang/rescript/pull/7315
3535
- Hide stdlib modules in output. https://github.com/rescript-lang/rescript/pull/7305
3636
- Deprecate unsafe host-specific bindings from stdlib. https://github.com/rescript-lang/rescript/pull/7334
37+
- Make unsafe function names consistent in Stdlib.String. https://github.com/rescript-lang/rescript/pull/7337
3738

3839
#### :bug: Bug fix
3940

runtime/Stdlib_String.res

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,41 @@ external unsafeReplaceRegExpBy3: (
9494
) => string,
9595
) => string = "replace"
9696

97+
@send
98+
external replaceRegExpBy0Unsafe: (
99+
string,
100+
Stdlib_RegExp.t,
101+
(~match: string, ~offset: int, ~input: string) => string,
102+
) => string = "replace"
103+
104+
@send
105+
external replaceRegExpBy1Unsafe: (
106+
string,
107+
Stdlib_RegExp.t,
108+
(~match: string, ~group1: string, ~offset: int, ~input: string) => string,
109+
) => string = "replace"
110+
111+
@send
112+
external replaceRegExpBy2Unsafe: (
113+
string,
114+
Stdlib_RegExp.t,
115+
(~match: string, ~group1: string, ~group2: string, ~offset: int, ~input: string) => string,
116+
) => string = "replace"
117+
118+
@send
119+
external replaceRegExpBy3Unsafe: (
120+
string,
121+
Stdlib_RegExp.t,
122+
(
123+
~match: string,
124+
~group1: string,
125+
~group2: string,
126+
~group3: string,
127+
~offset: int,
128+
~input: string,
129+
) => string,
130+
) => string = "replace"
131+
97132
@send external search: (string, Stdlib_RegExp.t) => int = "search"
98133
let searchOpt = (s, re) =>
99134
switch search(s, re) {

runtime/Stdlib_String.resi

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,7 @@ let matchFn = (~match, ~offset as _, ~input as _) => String.toUpperCase(match)
595595
String.unsafeReplaceRegExpBy0(str, re, matchFn) == "bEAUtIfUl vOwEls"
596596
```
597597
*/
598+
@deprecated("Use `replaceRegExpBy0Unsafe` instead")
598599
@send
599600
external unsafeReplaceRegExpBy0: (
600601
string,
@@ -618,6 +619,7 @@ let matchFn = (~match as _, ~group1, ~offset as _, ~input as _) => {
618619
String.unsafeReplaceRegExpBy1(str, re, matchFn) == "Jony is 41"
619620
```
620621
*/
622+
@deprecated("Use `replaceRegExpBy1Unsafe` instead")
621623
@send
622624
external unsafeReplaceRegExpBy1: (
623625
string,
@@ -644,6 +646,7 @@ let matchFn = (~match as _, ~group1, ~group2, ~offset as _, ~input as _) => {
644646
String.unsafeReplaceRegExpBy2(str, re, matchFn) == "42"
645647
```
646648
*/
649+
@deprecated("Use `replaceRegExpBy2Unsafe` instead")
647650
@send
648651
external unsafeReplaceRegExpBy2: (
649652
string,
@@ -652,10 +655,11 @@ external unsafeReplaceRegExpBy2: (
652655
) => string = "replace"
653656

654657
/**
655-
`unsafeReplaceRegExpBy3(str, regexp, f)`. Like `unsafeReplaceRegExpBy1`, but `f`
658+
`unsafeReplaceRegExpBy3(str, regexp, f)`. Like `unsafeReplaceRegExpBy2`, but `f`
656659
has three group parameters.
657660
See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN.
658661
*/
662+
@deprecated("Use `replaceRegExpBy3Unsafe` instead")
659663
@send
660664
external unsafeReplaceRegExpBy3: (
661665
string,
@@ -670,6 +674,98 @@ external unsafeReplaceRegExpBy3: (
670674
) => string,
671675
) => string = "replace"
672676

677+
/**
678+
`replaceRegExpBy0Unsafe(str, regex, f)` returns a new `string` with some or all
679+
matches of a pattern with no capturing parentheses replaced by the value
680+
returned from the given function. The function receives as its parameters the
681+
matched string, the offset at which the match begins, and the whole string being
682+
matched.
683+
See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN.
684+
685+
## Examples
686+
687+
```rescript
688+
let str = "beautiful vowels"
689+
let re = %re("/[aeiou]/g")
690+
let matchFn = (~match, ~offset as _, ~input as _) => String.toUpperCase(match)
691+
String.replaceRegExpBy0Unsafe(str, re, matchFn) == "bEAUtIfUl vOwEls"
692+
```
693+
*/
694+
@send
695+
external replaceRegExpBy0Unsafe: (
696+
string,
697+
Stdlib_RegExp.t,
698+
(~match: string, ~offset: int, ~input: string) => string,
699+
) => string = "replace"
700+
701+
/**
702+
`replaceRegExpBy1Unsafe(str, regexp, f)`. Like `replaceRegExpBy0Unsafe`, but `f`
703+
has `group1` parameter.
704+
See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN.
705+
706+
## Examples
707+
708+
```rescript
709+
let str = "Jony is 40"
710+
let re = %re("/(Jony is )\d+/g")
711+
let matchFn = (~match as _, ~group1, ~offset as _, ~input as _) => {
712+
group1 ++ "41"
713+
}
714+
String.replaceRegExpBy1Unsafe(str, re, matchFn) == "Jony is 41"
715+
```
716+
*/
717+
@send
718+
external replaceRegExpBy1Unsafe: (
719+
string,
720+
Stdlib_RegExp.t,
721+
(~match: string, ~group1: string, ~offset: int, ~input: string) => string,
722+
) => string = "replace"
723+
724+
/**
725+
`replaceRegExpBy2Unsafe(str, regexp, f)`. Like `replaceRegExpBy1Unsafe`, but `f`
726+
has two group parameters.
727+
See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN.
728+
729+
## Examples
730+
731+
```rescript
732+
let str = "7 times 6"
733+
let re = %re("/(\d+) times (\d+)/")
734+
let matchFn = (~match as _, ~group1, ~group2, ~offset as _, ~input as _) => {
735+
switch (Int.fromString(group1), Int.fromString(group2)) {
736+
| (Some(x), Some(y)) => Int.toString(x * y)
737+
| _ => "???"
738+
}
739+
}
740+
String.replaceRegExpBy2Unsafe(str, re, matchFn) == "42"
741+
```
742+
*/
743+
@send
744+
external replaceRegExpBy2Unsafe: (
745+
string,
746+
Stdlib_RegExp.t,
747+
(~match: string, ~group1: string, ~group2: string, ~offset: int, ~input: string) => string,
748+
) => string = "replace"
749+
750+
/**
751+
`replaceRegExpBy3Unsafe(str, regexp, f)`. Like `replaceRegExpBy2Unsafe`, but `f`
752+
has three group parameters.
753+
See [`String.replace`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace) on MDN.
754+
*/
755+
@send
756+
external replaceRegExpBy3Unsafe: (
757+
string,
758+
Stdlib_RegExp.t,
759+
(
760+
~match: string,
761+
~group1: string,
762+
~group2: string,
763+
~group3: string,
764+
~offset: int,
765+
~input: string,
766+
) => string,
767+
) => string = "replace"
768+
673769
/**
674770
`search(str, regexp)` returns the starting position of the first match of
675771
`regexp` in the given `str`, or -1 if there is no match.

0 commit comments

Comments
 (0)