haskell / haskell/text

Make `Data.Text.replace` total

Open
#425 6 comments 2 reactions 0 assignees View on GitHub
feature request question
Dominant language
Haskell
Stars
421
Forks
163
PR merge metrics
No merged PRs in 30d

Description

`Data.Text.replace` errors if the *needle* is the empty string. Every other implementation I tried matches the empty string against every position in the string. I suggest that for consistency with other languages and to avoiding partiality we adopt that behavior.

Expected behavior:

```haskell
ghci> replace "" "x" "foo"
"xfxoxox"
```
Actual behavior:
```haskell
ghci> replace "" "x" "foo"
"*** Exception: Data.Text.replace: empty input
CallStack (from HasCallStack):
error, called at libraries/text/src/Data/Text.hs:1862:18 in text-1.2.5.0:Data.Text
```

##### Rust:
```rust
fn main() {
println!("{}", "foo".replace("", "x")); // prints "xfxoxox"
}
```
```bash
$ rustc main.rs && ./main
xfxoxox
```

##### Python:
```python
>>> 'foo'.replace('', 'x')
'xfxoxox'
```
##### JavaScript:
```javascript
> 'foo'.replaceAll('', 'x')
'xfxoxox'
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in libraries/text/src/Data/Text.hs around line 1862, where the empty-needle error is raised, and inspect the existing replace behavior and tests. Done means replacing an empty needle produces the documented "xfxoxox" result for "foo" without throwing an exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
haskell
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.