Skip to content

Commit 187a0eb

Browse files
tsnobipfhammerschmidt
authored andcommitted
make unsafe function names consistent (#7337)
Fixes rescript-lang/rescript-core#225
1 parent c21660d commit 187a0eb

File tree

5 files changed

+179
-47
lines changed

5 files changed

+179
-47
lines changed

CHANGELOG.md

+1
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

+35
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

+97-1
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.

tests/analysis_tests/tests/src/expected/CompletionJsx.res.txt

+36-36
Original file line numberDiff line numberDiff line change
@@ -724,30 +724,6 @@ Path Stdlib.String.s
724724
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
725725
"newText": ""
726726
}]
727-
}, {
728-
"label": "->String.startsWith",
729-
"kind": 12,
730-
"tags": [],
731-
"detail": "(string, string) => bool",
732-
"documentation": {"kind": "markdown", "value": "\n`startsWith(str, substr)` returns `true` if the `str` starts with `substr`,\n`false` otherwise.\nSee [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN.\n\n## Examples\n\n```rescript\nString.startsWith(\"BuckleScript\", \"Buckle\") == true\nString.startsWith(\"BuckleScript\", \"\") == true\nString.startsWith(\"JavaScript\", \"Buckle\") == false\n```\n"},
733-
"sortText": "startsWith",
734-
"insertText": "->String.startsWith",
735-
"additionalTextEdits": [{
736-
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
737-
"newText": ""
738-
}]
739-
}, {
740-
"label": "->String.splitAtMost",
741-
"kind": 12,
742-
"tags": [],
743-
"detail": "(string, string, ~limit: int) => array<string>",
744-
"documentation": {"kind": "markdown", "value": "\n`splitAtMost(str, delimiter, ~limit)` splits the given `str` at every\noccurrence of `delimiter` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings,\nthe array will contain all the substrings.\n\n## Examples\n\n```rescript\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=3) == [\"ant\", \"bee\", \"cat\"]\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=0) == []\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=9) == [\"ant\", \"bee\", \"cat\", \"dog\", \"elk\"]\n```\n"},
745-
"sortText": "splitAtMost",
746-
"insertText": "->String.splitAtMost",
747-
"additionalTextEdits": [{
748-
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
749-
"newText": ""
750-
}]
751727
}, {
752728
"label": "->String.searchOpt",
753729
"kind": 12,
@@ -784,6 +760,42 @@ Path Stdlib.String.s
784760
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
785761
"newText": ""
786762
}]
763+
}, {
764+
"label": "->String.substringToEnd",
765+
"kind": 12,
766+
"tags": [],
767+
"detail": "(string, ~start: int) => string",
768+
"documentation": {"kind": "markdown", "value": "\n`substringToEnd(str, ~start)` returns the substring of `str` from position\n`start` to the end.\n- If `start` is less than or equal to zero, the entire string is returned.\n- If `start` is greater than or equal to the length of `str`, the empty string\nis returned.\nSee [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN.\n\n## Examples\n\n```rescript\nString.substringToEnd(\"playground\", ~start=4) == \"ground\"\nString.substringToEnd(\"playground\", ~start=-3) == \"playground\"\nString.substringToEnd(\"playground\", ~start=12) == \"\"\n```\n"},
769+
"sortText": "substringToEnd",
770+
"insertText": "->String.substringToEnd",
771+
"additionalTextEdits": [{
772+
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
773+
"newText": ""
774+
}]
775+
}, {
776+
"label": "->String.startsWith",
777+
"kind": 12,
778+
"tags": [],
779+
"detail": "(string, string) => bool",
780+
"documentation": {"kind": "markdown", "value": "\n`startsWith(str, substr)` returns `true` if the `str` starts with `substr`,\n`false` otherwise.\nSee [`String.startsWith`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) on MDN.\n\n## Examples\n\n```rescript\nString.startsWith(\"BuckleScript\", \"Buckle\") == true\nString.startsWith(\"BuckleScript\", \"\") == true\nString.startsWith(\"JavaScript\", \"Buckle\") == false\n```\n"},
781+
"sortText": "startsWith",
782+
"insertText": "->String.startsWith",
783+
"additionalTextEdits": [{
784+
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
785+
"newText": ""
786+
}]
787+
}, {
788+
"label": "->String.splitAtMost",
789+
"kind": 12,
790+
"tags": [],
791+
"detail": "(string, string, ~limit: int) => array<string>",
792+
"documentation": {"kind": "markdown", "value": "\n`splitAtMost(str, delimiter, ~limit)` splits the given `str` at every\noccurrence of `delimiter` and returns an array of the first `limit` resulting\nsubstrings. If `limit` is negative or greater than the number of substrings,\nthe array will contain all the substrings.\n\n## Examples\n\n```rescript\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=3) == [\"ant\", \"bee\", \"cat\"]\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=0) == []\nString.splitAtMost(\"ant/bee/cat/dog/elk\", \"/\", ~limit=9) == [\"ant\", \"bee\", \"cat\", \"dog\", \"elk\"]\n```\n"},
793+
"sortText": "splitAtMost",
794+
"insertText": "->String.splitAtMost",
795+
"additionalTextEdits": [{
796+
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
797+
"newText": ""
798+
}]
787799
}, {
788800
"label": "->String.sliceToEnd",
789801
"kind": 12,
@@ -868,17 +880,5 @@ Path Stdlib.String.s
868880
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
869881
"newText": ""
870882
}]
871-
}, {
872-
"label": "->String.substringToEnd",
873-
"kind": 12,
874-
"tags": [],
875-
"detail": "(string, ~start: int) => string",
876-
"documentation": {"kind": "markdown", "value": "\n`substringToEnd(str, ~start)` returns the substring of `str` from position\n`start` to the end.\n- If `start` is less than or equal to zero, the entire string is returned.\n- If `start` is greater than or equal to the length of `str`, the empty string\nis returned.\nSee [`String.substring`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substring) on MDN.\n\n## Examples\n\n```rescript\nString.substringToEnd(\"playground\", ~start=4) == \"ground\"\nString.substringToEnd(\"playground\", ~start=-3) == \"playground\"\nString.substringToEnd(\"playground\", ~start=12) == \"\"\n```\n"},
877-
"sortText": "substringToEnd",
878-
"insertText": "->String.substringToEnd",
879-
"additionalTextEdits": [{
880-
"range": {"start": {"line": 93, "character": 17}, "end": {"line": 93, "character": 18}},
881-
"newText": ""
882-
}]
883883
}]
884884

tests/analysis_tests/tests/src/expected/DotPipeCompletionSpec.res.txt

+10-10
Original file line numberDiff line numberDiff line change
@@ -386,25 +386,25 @@ ContextPath Value[Array, joinWith]
386386
Path Array.joinWith
387387
Path Stdlib.String.includ
388388
[{
389-
"label": "->String.includesFrom",
389+
"label": "->String.includes",
390390
"kind": 12,
391391
"tags": [],
392-
"detail": "(string, string, int) => bool",
393-
"documentation": {"kind": "markdown", "value": "\n`includesFrom(str, searchValue, start)` returns `true` if `searchValue` is found\nanywhere within `str` starting at character number `start` (where 0 is the\nfirst character), `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includesFrom(\"programmer\", \"gram\", 1) == true\nString.includesFrom(\"programmer\", \"gram\", 4) == false\nString.includesFrom(`대한민국`, `한`, 1) == true\n```\n"},
394-
"sortText": "includesFrom",
395-
"insertText": "->String.includesFrom",
392+
"detail": "(string, string) => bool",
393+
"documentation": {"kind": "markdown", "value": "\n`includes(str, searchValue)` returns `true` if `searchValue` is found anywhere\nwithin `str`, `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includes(\"programmer\", \"gram\") == true\nString.includes(\"programmer\", \"er\") == true\nString.includes(\"programmer\", \"pro\") == true\nString.includes(\"programmer.dat\", \"xyz\") == false\n```\n"},
394+
"sortText": "includes",
395+
"insertText": "->String.includes",
396396
"additionalTextEdits": [{
397397
"range": {"start": {"line": 89, "character": 55}, "end": {"line": 89, "character": 56}},
398398
"newText": ""
399399
}]
400400
}, {
401-
"label": "->String.includes",
401+
"label": "->String.includesFrom",
402402
"kind": 12,
403403
"tags": [],
404-
"detail": "(string, string) => bool",
405-
"documentation": {"kind": "markdown", "value": "\n`includes(str, searchValue)` returns `true` if `searchValue` is found anywhere\nwithin `str`, `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includes(\"programmer\", \"gram\") == true\nString.includes(\"programmer\", \"er\") == true\nString.includes(\"programmer\", \"pro\") == true\nString.includes(\"programmer.dat\", \"xyz\") == false\n```\n"},
406-
"sortText": "includes",
407-
"insertText": "->String.includes",
404+
"detail": "(string, string, int) => bool",
405+
"documentation": {"kind": "markdown", "value": "\n`includesFrom(str, searchValue, start)` returns `true` if `searchValue` is found\nanywhere within `str` starting at character number `start` (where 0 is the\nfirst character), `false` otherwise.\nSee [`String.includes`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes) on MDN.\n\n## Examples\n\n```rescript\nString.includesFrom(\"programmer\", \"gram\", 1) == true\nString.includesFrom(\"programmer\", \"gram\", 4) == false\nString.includesFrom(`대한민국`, `한`, 1) == true\n```\n"},
406+
"sortText": "includesFrom",
407+
"insertText": "->String.includesFrom",
408408
"additionalTextEdits": [{
409409
"range": {"start": {"line": 89, "character": 55}, "end": {"line": 89, "character": 56}},
410410
"newText": ""

0 commit comments

Comments
 (0)