Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(stdlib): Add String.repeat to String module #2140

Merged
merged 7 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/test/stdlib/string.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -551,3 +551,12 @@ assert String.toAsciiLowercase("aBc🌾12Y") == "abc🌾12y"

// toAsciiUppercase
assert String.toAsciiUppercase("aBc🌾12Y") == "ABC🌾12Y"

// String.repeat
assert String.repeat("=.", 1) == "=."
assert String.repeat("=", 10) == "=========="
assert String.repeat("=.", 10) == "=.=.=.=.=.=.=.=.=.=."
assert String.repeat("=.", 0) == ""
assert String.repeat("", 0) == ""
assert String.repeat("=.", -1) == ".="
assert String.repeat("=.", -10) == ".=.=.=.=.=.=.=.=.=.="
42 changes: 42 additions & 0 deletions stdlib/string.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2338,3 +2338,45 @@ provide let toAsciiUppercase = string => {
}
implode(chars)
}

/**
* Produces a new string by repeating a given substring a given number of times.
ospencer marked this conversation as resolved.
Show resolved Hide resolved
*
* @param string: The string to repeat
* @param count: The number of times to repeat, negative values reverse the substring
* @returns A string containing the repeated input string
*
* @throws InvalidArgument(String): When the `count` is not an integer
*
* @example assert String.repeat("=", 5) == "====="
* @example assert String.repeat("ab", -5) == "bababababa"
* @example assert String.repeat(".", 0) == ""
*
* @since v0.6.7
*/
@unsafe
provide let repeat = (string, count) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably reverse the arguments to put the string last.

use WasmI32.{ (+), (*), (<), (&), (!=) }
if ((WasmI32.fromGrain(count) & 1n) != 1n) {
throw InvalidArgument("Invalid count value")
}
let mut rawCount = untagSimpleNumber(count)
if (WasmI32.eqz(rawCount)) return ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I personally wouldn't have these eqz checks. They make the implementation more complex with little benefit (people are likely calling this function on non-empty strings with count > 0 a vast majority of the time). The code still produces the same result without them.

let string = if (rawCount < 0n) {
rawCount *= -1n
reverse(string)
spotandjake marked this conversation as resolved.
Show resolved Hide resolved
} else {
string
}
let mut stringPtr = WasmI32.fromGrain(string)
let byteLength = WasmI32.load(stringPtr, 4n)
if (WasmI32.eqz(byteLength)) return ""
stringPtr += 8n
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we don't ever mutate this again, it's probably better to just make a new binding, let stringPtr = stringPtr + 8n.

let strPtr = allocateString(byteLength * rawCount)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should rename this to something more clear, this is too similar to stringPtr

let strContentPtr = strPtr + 8n
for (let mut i = 0n; i < rawCount; i += 1n) {
Memory.copy(strContentPtr + byteLength * i, stringPtr, byteLength)
}
ignore(stringPtr)
ospencer marked this conversation as resolved.
Show resolved Hide resolved
return WasmI32.toGrain(strPtr): String
}
46 changes: 46 additions & 0 deletions stdlib/string.md
Original file line number Diff line number Diff line change
Expand Up @@ -1174,3 +1174,49 @@ Examples:
assert String.toAsciiUppercase("aBc123") == "ABC123"
```

### String.**repeat**

<details disabled>
<summary tabindex="-1">Added in <code>next</code></summary>
No other changes yet.
</details>

```grain
repeat : (string: String, count: Number) => String
```

Produces a new string by repeating a given substring a given number of times.

Parameters:

|param|type|description|
|-----|----|-----------|
|`string`|`String`|The string to repeat|
|`count`|`Number`|The number of times to repeat, negative values reverse the substring|

Returns:

|type|description|
|----|-----------|
|`String`|A string containing the repeated input string|

Throws:

`InvalidArgument(String)`

* When the `count` is not an integer

Examples:

```grain
assert String.repeat("=", 5) == "====="
```

```grain
assert String.repeat("ab", -5) == "bababababa"
```

```grain
assert String.repeat(".", 0) == ""
```

Loading