bazelbuild / bazelbuild/starlark
string.strip() discrepancies between Go and Java when handling multi-element unicode code points
- Dominant language
- Python
- Stars
- 3.1k
- Forks
- 178
- PR merge metrics
- No merged PRs in 30d
Description
The spec for `strip`, `lstrip`, and `rstrip` says
> It accepts an optional string argument, cutset, which instead removes all leading and trailing Unicode code points contained in cutset.
The Go implementation does seem to treat multi-element code points as a unit when passed to `strip`, which I think this is correct per the spec. If the left hand side is a full code point, passing a subset of that codepoint's elements in the right hand side has no effect. Similarly, if the left hand side is just a single element, passing the full code point on the right hand side has no effect.
```
>>> x = "😀"
>>> len(x)
4
>>> x.strip(x[0])
"😀"
>>> x.strip(x[0]+x[1])
"😀"
>>> x.strip(x[0]+x[1]+x[2])
"😀"
>>> x.strip(x[0]+x[1]+x[2]+x[3])
""
>>> x[0].strip(x)
"\xf0"
>>> x[0].strip(x[0]+x[1])
""
```
The Java implementation doesn't show the same behavior with full code points, instead just treating everything as a list of elements.
```
>> x = "😀"
>> len(x)
2
>> x.strip(x[0])
"?"
>> x.strip(x[0] + x[1])
""
>> x[0].strip(x)
""
```
However, according to the spec, `strip` stands out as the only method that I've found that treats full code points as something other than just a list of elements. For example, in the Go implementation, reversing a string containing a code point reverses the elements of that code point:
```
>>> x = "😀"
>>> x[::-1]
"\x80\x98\x9f\xf0"
```
The Java `strip` implementation feels more consistent with everything else in that it treats strings as a list of UTF-K elements, so maybe its approach should be preferred.
Contributor guide
Research direction
Start with the strip, lstrip, and rstrip specification and reproduce the provided multi-element Unicode examples in the Go and Java implementations. Determine whether the spec or one implementation should change, then confirm that the chosen behavior is documented and consistent across both implementations.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, java
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 30/100