Skip to content

strings: Split is inconsistent with bytes.Split #53511

Closed

Description

@dsnet

Consider the following:

in := "\xff-\xff"
sep := ""
fmt.Printf("%q\n", bytes.Split([]byte(in), []byte(sep)))   // ["\xff" "-" "\xff"]
fmt.Printf("%q\n", strings.Split(string(in), string(sep))) // ["�" "-" "\xff"]

The results of these two are inconsistent where the strings implementation replaced
\xff with utf8.RuneError, even though the documentation of strings.Split
mentions no such behavior. It only says that it slices up the input.
Furthermore, it is odd, that it only mangles the first element of the result, but not the last.

Activity

  1. gopherbot commented on Jun 23, 2022

    @gopherbot
    Contributor

    Change https://go.dev/cl/413715 mentions this issue: strings: avoid utf.RuneError mangling in Split

  2. added
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Jun 24, 2022
  3. added this to the Backlog milestone on Jun 24, 2022
  4. cagedmantis commented on Jun 24, 2022

    @cagedmantis
    Contributor
  5. added
    NeedsFixThe path to resolution is known, but the work has not been done.
    and removed
    NeedsInvestigationSomeone must examine and confirm this is a valid issue and not a duplicate of an existing one.
    on Jun 25, 2022
  6. modified the milestones: Backlog, Go1.19 on Jun 25, 2022
  7. nightlyone commented on Jul 13, 2022

    @nightlyone
    Contributor

    https://go.dev/play/p/7PPxD1AyxTq shows it together with 2 other situations where utf8 rune splitting is being done that are handled differently again.

    To me it looks like the strings and bytes package are both being inconsistent to each of the other documented rune splitting methods and probably both need to be adjusted.

    Or maybe I am missing something here and those functions are supposed to support rune splitting in a different way in order to allow for edge cases I am not aware of.

  8. dsnet commented on Jul 14, 2022

    @dsnet
    MemberAuthor

    @nightlyone. My CL makes it such that your example prints:

    ["\xff" "-" "\xff"]
    ["\xff" "-" "\xff"]
    ['�' '-' '�']
    '�''-''�'
    

    This brings the bytes and strings package in conformance to each other.

    Regarding the last two lines in your example, there is no documented guarantee that strings.Split(s, "") needs to be identical to []rune(s). In fact, strings.Split is technically bound by the documentation to always produce a list of subslices. Converting 0xff to the rune error violates that. On the other hand, []rune(s) is documented by the language specification that it does do the rune error mangling.

  9. modified the milestones: Go1.19, Go1.20 on Aug 2, 2022
  10. added a commit that references this issue on Aug 10, 2022
    aa6561e
  11. locked and limited conversation to collaborators on Aug 8, 2023
  12. added a commit that references this issue on Feb 10, 2024
    9a4685f
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    FrozenDueToAgeNeedsFixThe path to resolution is known, but the work has not been done.

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions