Skip to content

Commit

Permalink
Use .first(n) consistently rather than slice(0, n)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell committed Oct 10, 2024
1 parent bc19cc7 commit fa21559
Show file tree
Hide file tree
Showing 30 changed files with 85 additions and 84 deletions.
2 changes: 1 addition & 1 deletion src/workerd/api/actor-state.c++
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ jsg::JsValue deserializeV8Value(
kj::String actorId = getCurrentActorId().orDefault([]() { return kj::String(); });
KJ_FAIL_ASSERT("actor storage deserialization failed", "failed to deserialize stored value",
actorId, exception.getHandle(js), key, buf.size(),
buf.slice(0, std::min(static_cast<size_t>(3), buf.size())));
buf.first(std::min(static_cast<size_t>(3), buf.size())));
});
} catch (jsg::JsExceptionThrown&) {
// We can occasionally hit an isolate termination here -- we prefix the error with jsg to avoid
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/crypto/aes.c++
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private:
plainSize = 0;
}

auto actualCipherText = cipherText.slice(0, cipherText.size() - tagLength / 8);
auto actualCipherText = cipherText.first(cipherText.size() - tagLength / 8);
auto tagText = cipherText.slice(actualCipherText.size(), cipherText.size());

auto plainText = kj::heapArray<kj::byte>(actualCipherText.size());
Expand Down Expand Up @@ -513,7 +513,7 @@ protected:
// counter portion of the block back to zero.
auto inputSizePart1 = BN_get_word(numBlocksUntilReset.get()) * AES_BLOCK_SIZE;

process(&cipher, data.slice(0, inputSizePart1), counter, result.asPtr());
process(&cipher, data.first(inputSizePart1), counter, result.asPtr());

// Zero the counter bits of the block. Chromium creates a copy but we own our buffer.
{
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/crypto/digest.c++
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ private:

if (format == "jwk") {
// This assert enforces that the slice logic to fill in `.alg` below is safe.
JSG_REQUIRE(keyAlgorithm.hash.name.slice(0, 4) == "SHA-"_kj, DOMNotSupportedError,
JSG_REQUIRE(keyAlgorithm.hash.name.first(4) == "SHA-"_kj, DOMNotSupportedError,
"Unimplemented JWK key export format for key algorithm \"", keyAlgorithm.hash.name,
"\".");

Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/crypto/ec.c++
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ public:
return nullptr;
}

auto r = signature.slice(0, rsSize);
auto r = signature.first(rsSize);
auto s = signature.slice(rsSize, signature.size());

// Trim leading zeros.
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/crypto/keys.c++
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ kj::Array<kj::byte> AsymmetricKeyCryptoKeyImpl::sign(

KJ_ASSERT(signatureSize <= signature.size());
if (signatureSize < signature.size()) {
signature = kj::heapArray<kj::byte>(signature.slice(0, signatureSize));
signature = kj::heapArray<kj::byte>(signature.first(signatureSize));
}

return signatureSslToWebCrypto(kj::mv(signature));
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/crypto/rsa.c++
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ kj::Array<kj::byte> Rsa::sign(const kj::ArrayPtr<const kj::byte> data) const {
// We did not fill the entire buffer, let's make sure we zero
// out the rest of it so we don't leak any uninitialized data.
signature.slice(signatureSize).fill(0);
return signature.slice(0, signatureSize).attach(kj::mv(signature));
return signature.first(signatureSize).attach(kj::mv(signature));
}

return kj::mv(signature);
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/eventsource.c++
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public:
KJ_IF_SOME(found, findEndOfLine(input)) {
auto prefix = kept.releaseAsArray();
// Feed the line into the processor.
feed(kj::str(prefix, input.slice(0, found.pos)));
feed(kj::str(prefix, input.first(found.pos)));
input = found.remaining;
// If we've reached the end of the input, input will == nullptr here.
} else {
Expand Down Expand Up @@ -168,7 +168,7 @@ private:
};

KJ_IF_SOME(pos, line.findFirst(':')) {
handle(*this, line.slice(0, pos), line.slice(pos + 1));
handle(*this, line.first(pos), line.slice(pos + 1));
} else {
handle(*this, line, ""_kjc);
}
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/form-data.c++
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ void parseFormData(kj::Maybe<jsg::Lock&> js,
//
// TODO(soon): Read the Content-Type to support files.

auto headersText = kj::str(body.slice(0, match[0].second - body.begin()));
auto headersText = kj::str(body.first(match[0].second - body.begin()));
body = body.slice(match[0].second - body.begin(), body.size());

kj::HttpHeaders headers(*formDataHeaderTable.table);
Expand Down Expand Up @@ -163,7 +163,7 @@ void parseFormData(kj::Maybe<jsg::Lock&> js,

if (message.size() > 0) {
// If we skipped a CR, we must avoid including it in the message data.
message = message.slice(0, message.size() - uint(message.back() == '\r'));
message = message.first(message.size() - uint(message.back() == '\r'));
}

if (filename == kj::none || convertFilesToStrings) {
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ jsg::JsString ServiceWorkerGlobalScope::btoa(jsg::Lock& js, jsg::JsValue data) {
auto result = kj::heapArray<kj::byte>(expected_length);
auto written = simdutf::binary_to_base64(
strArray.asChars().begin(), strArray.size(), result.asChars().begin());
return js.str(result.slice(0, written).attach(kj::mv(result)));
return js.str(result.first(written).attach(kj::mv(result)));
}
jsg::JsString ServiceWorkerGlobalScope::atob(jsg::Lock& js, kj::String data) {
auto decoded = kj::decodeBase64(data.asArray());
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ jsg::ByteString normalizeHeaderValue(jsg::ByteString value) {
slice = slice.slice(1, slice.size());
}
while (slice.size() > 0 && isHttpWhitespace(slice.back())) {
slice = slice.slice(0, slice.size() - 1);
slice = slice.first(slice.size() - 1);
}
if (slice.size() == value.size()) {
return kj::mv(value);
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ class Body: public jsg::Object {
: ownBytes(kj::refcounted<RefcountedBytes>(string.releaseArray().releaseAsBytes())),
view([this] {
auto bytesIncludingNull = ownBytes.get<kj::Own<RefcountedBytes>>()->bytes.asPtr();
return bytesIncludingNull.slice(0, bytesIncludingNull.size() - 1);
return bytesIncludingNull.first(bytesIncludingNull.size() - 1);
}()) {}
Buffer(jsg::Ref<Blob> blob)
: ownBytes(kj::mv(blob)),
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/node/buffer.c++
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kj::Array<byte> decodeHexTruncated(kj::ArrayPtr<kj::byte> text, bool strict = fa
if (strict) {
JSG_FAIL_REQUIRE(TypeError, "The text is not valid hex");
}
text = text.slice(0, text.size() - 1);
text = text.first(text.size() - 1);
}
auto vec = kj::Vector<kj::byte>(text.size() / 2);

Expand Down Expand Up @@ -159,7 +159,7 @@ kj::Array<kj::byte> decodeStringImpl(
auto len = result.written;
auto dest = kj::heapArray<kj::byte>(nbytes::Base64DecodedSize(buf.begin(), len));
len = nbytes::Base64Decode(dest.asChars().begin(), dest.size(), buf.begin(), buf.size());
return dest.slice(0, len).attach(kj::mv(dest));
return dest.first(len).attach(kj::mv(dest));
}
case Encoding::HEX: {
KJ_STACK_ARRAY(kj::byte, buf, length, 1024, 536870888);
Expand Down
6 changes: 3 additions & 3 deletions src/workerd/api/node/i18n.c++
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ kj::Maybe<kj::Array<kj::byte>> TranscodeDefault(
ucnv_convertEx(to.conv(), from.conv(), &target, target + limit, &source_, source_ + source.size(),
nullptr, nullptr, nullptr, nullptr, true, true, &status);
if (U_SUCCESS(status)) {
return out.slice(0, target - out.asChars().begin()).attach(kj::mv(out));
return out.first(target - out.asChars().begin()).attach(kj::mv(out));
}

return kj::none;
Expand All @@ -82,7 +82,7 @@ kj::Maybe<kj::Array<kj::byte>> TranscodeLatin1ToUTF16(
return kj::none;
}

return destbuf.slice(0, actual_length).asBytes().attach(kj::mv(destbuf));
return destbuf.first(actual_length).asBytes().attach(kj::mv(destbuf));
}

kj::Maybe<kj::Array<kj::byte>> TranscodeFromUTF16(
Expand All @@ -105,7 +105,7 @@ kj::Maybe<kj::Array<kj::byte>> TranscodeFromUTF16(
utf16_input.begin(), utf16_input.size(), &status);

if (U_SUCCESS(status)) {
return destbuf.slice(0, len).asBytes().attach(kj::mv(destbuf));
return destbuf.first(len).asBytes().attach(kj::mv(destbuf));
}

return kj::none;
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/streams/internal.c++
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ private:
// In which case we want to add that subset to the parts list here before we exit
// the loop.
if (amount > 0) {
parts.add(bytes.slice(0, amount).attach(kj::mv(bytes)));
parts.add(bytes.first(amount).attach(kj::mv(bytes)));
}
break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/streams/standard.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2654,7 +2654,7 @@ public:
jsg::Promise<kj::String> allText(jsg::Lock& js) {
return loop(js).then(js, [this](auto& js, PartList&& partPtrs) {
auto out = kj::heapArray<char>(runningTotal + 1);
copyInto(out.slice(0, out.size() - 1).asBytes(), kj::mv(partPtrs));
copyInto(out.first(out.size() - 1).asBytes(), kj::mv(partPtrs));
out.back() = '\0';
return kj::String(kj::mv(out));
});
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/streams/writable.c++
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,7 @@ public:
KJ_DASSERT(ptr.size() > 0);
KJ_DASSERT(piece.size() <= ptr.size());
if (piece.size() == 0) continue;
ptr.slice(0, piece.size()).copyFrom(piece);
ptr.first(piece.size()).copyFrom(piece);
ptr = ptr.slice(piece.size());
}

Expand Down
16 changes: 8 additions & 8 deletions src/workerd/api/url.c++
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ void normalizePort(kj::Url& url) {
KJ_IF_SOME(colon, url.host.findFirst(':')) {
if (url.host.size() == colon + 1) {
// Remove trailing ':'.
url.host = kj::str(url.host.slice(0, colon));
url.host = kj::str(url.host.first(colon));
} else KJ_IF_SOME(defaultPort, defaultPortForScheme(url.scheme)) {
if (defaultPort == url.host.slice(colon + 1)) {
// Remove scheme-default port.
url.host = kj::str(url.host.slice(0, colon));
url.host = kj::str(url.host.first(colon));
}
}
}
Expand All @@ -84,7 +84,7 @@ kj::Maybe<kj::ArrayPtr<const char>> trySplit(kj::ArrayPtr<const char>& text, cha

for (auto i: kj::indices(text)) {
if (text[i] == c) {
kj::ArrayPtr<const char> result = text.slice(0, i);
kj::ArrayPtr<const char> result = text.first(i);
text = text.slice(i + 1, text.size());
return result;
}
Expand All @@ -97,7 +97,7 @@ kj::ArrayPtr<const char> split(kj::StringPtr& text, const kj::parse::CharGroup_&

for (auto i: kj::indices(text)) {
if (chars.contains(text[i])) {
kj::ArrayPtr<const char> result = text.slice(0, i);
kj::ArrayPtr<const char> result = text.first(i);
text = text.slice(i);
return result;
}
Expand Down Expand Up @@ -239,7 +239,7 @@ kj::String URL::getProtocol() {
}
void URL::setProtocol(kj::String value) {
KJ_IF_SOME(colon, value.findFirst(':')) {
value = kj::str(value.slice(0, colon));
value = kj::str(value.first(colon));
}

auto copy = url->clone();
Expand Down Expand Up @@ -333,14 +333,14 @@ void URL::setHost(kj::String value) {

kj::String URL::getHostname() {
KJ_IF_SOME(colon, url->host.findFirst(':')) {
return kj::str(url->host.slice(0, colon));
return kj::str(url->host.first(colon));
}
return kj::str(url->host);
}
void URL::setHostname(kj::String value) {
// In contrast to the host setter, the hostname setter explicitly ignores any new port. We take
// the hostname from the new value and the port from the old value.
auto hostnameString = value.slice(0, value.findFirst(':').orDefault(value.size()));
auto hostnameString = value.first(value.findFirst(':').orDefault(value.size()));
auto portString = url->host.slice(url->host.findFirst(':').orDefault(url->host.size()));

url->host = kj::str(hostnameString, portString);
Expand All @@ -355,7 +355,7 @@ kj::String URL::getPort() {
void URL::setPort(kj::String value) {
KJ_IF_SOME(colon, url->host.findFirst(':')) {
// Our url's host already has a port. Replace it.
value = kj::str(url->host.slice(0, colon + 1), kj::mv(value));
value = kj::str(url->host.first(colon + 1), kj::mv(value));
} else {
value = kj::str(url->host, ':', kj::mv(value));
}
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/urlpattern.c++
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ kj::Maybe<URLPattern::URLPatternResult> URLPattern::exec(
return s.asPtr();
}))) {
auto p = url.getProtocol();
protocol = kj::str(p.slice(0, p.size() - 1));
protocol = kj::str(p.first(p.size() - 1));
username = kj::str(url.getUsername());
password = kj::str(url.getPassword());
hostname = kj::str(url.getHostname());
Expand Down
4 changes: 2 additions & 2 deletions src/workerd/api/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ kj::ArrayPtr<const char> split(kj::ArrayPtr<const char>& text, char c) {

for (auto i: kj::indices(text)) {
if (text[i] == c) {
kj::ArrayPtr<const char> result = text.slice(0, i);
kj::ArrayPtr<const char> result = text.first(i);
text = text.slice(i + 1, text.size());
return result;
}
Expand Down Expand Up @@ -235,7 +235,7 @@ kj::Array<char16_t> fastEncodeUtf16(kj::ArrayPtr<const char> bytes) {
auto output = kj::heapArray<char16_t>(expected_length);
auto actual_length =
simdutf::convert_utf8_to_utf16(bytes.asChars().begin(), bytes.size(), output.begin());
return output.slice(0, actual_length).attach(kj::mv(output));
return output.first(actual_length).attach(kj::mv(output));
}

} // namespace workerd::api
2 changes: 1 addition & 1 deletion src/workerd/io/trace.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ class WorkerTracer final: public kj::Refcounted {
// Helper function used when setting "truncated_script_id" tags. Truncates the scriptId to 10
// characters.
inline kj::String truncateScriptId(kj::StringPtr id) {
auto truncatedId = id.slice(0, kj::min(id.size(), 10));
auto truncatedId = id.first(kj::min(id.size(), 10));
return kj::str(truncatedId);
}

Expand Down
Loading

0 comments on commit fa21559

Please sign in to comment.