-
-
Notifications
You must be signed in to change notification settings - Fork 113
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
feat(stdlib): Add String.repeat
to String
module
#2140
Conversation
I personally think that |
stdlib/string.gr
Outdated
* @since v0.6.7 | ||
*/ | ||
@unsafe | ||
provide let repeat = (string, count) => { |
There was a problem hiding this comment.
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.
stdlib/string.gr
Outdated
throw InvalidArgument("Invalid count value") | ||
} | ||
let mut rawCount = untagSimpleNumber(count) | ||
if (WasmI32.eqz(rawCount)) return "" |
There was a problem hiding this comment.
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.
stdlib/string.gr
Outdated
let mut stringPtr = WasmI32.fromGrain(string) | ||
let byteLength = WasmI32.load(stringPtr, 4n) | ||
if (WasmI32.eqz(byteLength)) return "" | ||
stringPtr += 8n |
There was a problem hiding this comment.
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
.
stdlib/string.gr
Outdated
let byteLength = WasmI32.load(stringPtr, 4n) | ||
if (WasmI32.eqz(byteLength)) return "" | ||
stringPtr += 8n | ||
let strPtr = allocateString(byteLength * rawCount) |
There was a problem hiding this comment.
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
I applied those changes |
@spotandjake Can you regen the docs? |
I pushed the updated graindoc |
This pr adds
String.repeat
to the standard library.Should this be called
String.make
? It would align better with theArray
library andArray.make
the behaviour is similar.