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

chore(stdlib): Optimize String.trim #1965

Merged
merged 1 commit into from
Jan 26, 2024
Merged
Changes from all 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
57 changes: 41 additions & 16 deletions stdlib/string.gr
Original file line number Diff line number Diff line change
Expand Up @@ -2032,12 +2032,8 @@ provide let forEachCodePointi = (fn: (Number, Number) => Void, str: String) => {
}

@unsafe
let trimString = (str: String, fromEnd: Bool) => {
let trimString = (stringPtr: WasmI32, byteLength: WasmI32, fromEnd: Bool) => {
from WasmI32 use { (+), (-), (*), (>>>), ltU as (<), (==), (!=) }

let mut stringPtr = WasmI32.fromGrain(str)
let byteLength = WasmI32.load(stringPtr, 4n)
stringPtr += 8n
let mut i = 0n
and offset = 1n
if (fromEnd) {
Expand Down Expand Up @@ -2083,14 +2079,7 @@ let trimString = (str: String, fromEnd: Bool) => {
}
count += 1n
}
let str = allocateString(byteLength - count)
// Copy the string
if (fromEnd) {
Memory.copy(str + 8n, stringPtr, byteLength - count)
} else {
Memory.copy(str + 8n, stringPtr + count, byteLength - count)
}
WasmI32.toGrain(str): String
count
}
/**
* Trims the beginning of a string—removing any leading whitespace characters.
Expand All @@ -2102,7 +2091,17 @@ let trimString = (str: String, fromEnd: Bool) => {
*
* @since v0.4.2
*/
provide let trimStart = (string: String) => trimString(string, false)
@unsafe
provide let trimStart = (string: String) => {
from WasmI32 use { (-), (+) }
let mut stringPtr = WasmI32.fromGrain(string)
let byteLength = WasmI32.load(stringPtr, 4n)
stringPtr += 8n
let count = trimString(stringPtr, byteLength, false)
let str = allocateString(byteLength - count)
Memory.copy(str + 8n, stringPtr + count, byteLength - count)
WasmI32.toGrain(str): String
}
/**
* Trims the end of a string—removing any trailing whitespace characters.
*
Expand All @@ -2113,7 +2112,17 @@ provide let trimStart = (string: String) => trimString(string, false)
*
* @since v0.4.2
*/
provide let trimEnd = (string: String) => trimString(string, true)
@unsafe
provide let trimEnd = (string: String) => {
from WasmI32 use { (-), (+) }
let mut stringPtr = WasmI32.fromGrain(string)
let byteLength = WasmI32.load(stringPtr, 4n)
stringPtr += 8n
let count = trimString(stringPtr, byteLength, true)
let str = allocateString(byteLength - count)
Memory.copy(str + 8n, stringPtr, byteLength - count)
WasmI32.toGrain(str): String
}
/**
* Trims a string—removing all leading and trailing whitespace characters.
*
Expand All @@ -2124,4 +2133,20 @@ provide let trimEnd = (string: String) => trimString(string, true)
*
* @since v0.4.2
*/
provide let trim = (string: String) => trimEnd(trimStart(string))
@unsafe
provide let trim = (string: String) => {
from WasmI32 use { (-), (+), (==) }
let mut stringPtr = WasmI32.fromGrain(string)
let byteLength = WasmI32.load(stringPtr, 4n)
stringPtr += 8n
let startCount = trimString(stringPtr, byteLength, false)
if (startCount == byteLength) return ""
let endCount = trimString(stringPtr, byteLength, true)
let str = allocateString(byteLength - startCount - endCount)
Memory.copy(
str + 8n,
stringPtr + startCount,
byteLength - startCount - endCount
)
return WasmI32.toGrain(str): String
}
Loading