Skip to content

String.words of a string with no words in it returns a 1-member Array instead of an empty Array #151

Description

@gilramir

Found against: gren 0.6.6, gren-lang/core 7.4.2, node 22

Summary

String.words's docs say this:

Break a string into words, splitting on chunks of whitespace.

When the string it parses has 2 words, the resulting Array has 2 items.
When the string has 1 word, it returns an Array of 1 item, regardless of whitespace before or after.

Array.length (String.words "a b")   -- 2
Array.length (String.words " a ")   -- 1, the padding dropped

However when the input has no words, the result is not an empty Array.
Instead it is an Array of one empty string.

Array.length (String.words "")      -- 1
Array.first  (String.words "")      -- Just ""

The reason I believe this is a defect rather than a choice is that words never returns
empty strings anywhere else. " a " does not answer [ "", "a", "" ]; a run of
whitespace between two words does not return empty strings between them.

Every caller can therefore assume that a word is a non-empty string.

Looking at it another way, it's odd that the length of words "a" is the same length of words ""

> String.words "a b c"
["a", "b", "c"] : Array String

> String.words "a b"
["a", "b"] : Array String

> String.words "a"
["a"] : Array String

> String.words ""
[""] : Array String

Activity

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

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions