Skip to content

Commit

Permalink
merge isAsciiWhitespace implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Sep 25, 2024
1 parent b099721 commit b2ed219
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 41 deletions.
53 changes: 53 additions & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ filegroup(
exclude = [
"html-rewriter.c++",
"data-url.c++",
"encoding.c++",
"util.c++",
"util-iocontext.c++",
"rtti.c++",
"**/*test*.c++",
"pyodide.c++",
Expand All @@ -23,6 +26,9 @@ filegroup(
exclude = [
"html-rewriter.c++",
"data-url.c++",
"encoding.c++",
"util.c++",
"util-iocontext.c++",
"rtti.c++",
"**/*test*.c++",
"pyodide.c++",
Expand All @@ -38,6 +44,8 @@ api_header_exclusions = [
"html-rewriter.h",
"deferred-proxy.h",
"data-url.h",
"encoding.h",
"util.h",
"modules.h",
"rtti.h",
"**/*test*.h",
Expand Down Expand Up @@ -121,10 +129,48 @@ kj_test(
],
)

wd_cc_library(
name = "util",
srcs = ["util.c++"],
hdrs = ["util.h"],
visibility = ["//visibility:public"],
deps = [
"//src/workerd/jsg",
"//src/workerd/util",
"@capnp-cpp//src/kj",
"@simdutf",
],
)

wd_cc_library(
name = "util-iocontext",
srcs = ["util-iocontext.c++"],
hdrs = ["util-iocontext.h"],
implementation_deps = ["//src/workerd/io"],
visibility = ["//visibility:public"],
)

wd_cc_library(
name = "encoding",
srcs = ["encoding.c++"],
hdrs = ["encoding.h"],
visibility = ["//visibility:public"],
deps = [
":util",
"//src/workerd/io:compatibility_date_capnp",
"//src/workerd/jsg",
"//src/workerd/util",
"@capnp-cpp//src/kj",
],
)

wd_cc_library(
name = "data-url",
srcs = ["data-url.c++"],
hdrs = ["data-url.h"],
implementation_deps = [
":encoding",
],
visibility = ["//visibility:public"],
deps = [
"//src/workerd/jsg:url",
Expand Down Expand Up @@ -159,6 +205,8 @@ wd_cc_capnp_library(
kj_test(
src = f,
deps = [
":encoding",
":util-iocontext",
"//src/workerd/io",
],
)
Expand All @@ -177,6 +225,7 @@ kj_test(
src = "data-url-test.c++",
deps = [
":data-url",
":encoding",
],
)

Expand All @@ -190,6 +239,7 @@ kj_test(
kj_test(
src = "streams/internal-test.c++",
deps = [
":util-iocontext",
"//src/workerd/io",
"//src/workerd/tests:test-fixture",
],
Expand All @@ -198,6 +248,7 @@ kj_test(
kj_test(
src = "actor-state-iocontext-test.c++",
deps = [
":util-iocontext",
"//src/workerd/io",
"//src/workerd/tests:test-fixture",
],
Expand All @@ -206,7 +257,9 @@ kj_test(
kj_test(
src = "api-rtti-test.c++",
deps = [
":encoding",
":html-rewriter",
":util-iocontext",
"//src/workerd/io",
"//src/workerd/jsg:rtti",
],
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/analytics-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#pragma once

#include <workerd/api/analytics-engine-impl.h>
#include <workerd/api/analytics-engine.capnp.h>
#include <workerd/api/util-iocontext.h>
#include <workerd/api/util.h>
#include <workerd/jsg/jsg.h>

Expand Down
14 changes: 2 additions & 12 deletions src/workerd/api/data-url.c++
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "data-url.h"

#include <workerd/api/encoding.h>

#include <kj/encoding.h>

namespace workerd::api {
Expand All @@ -11,18 +13,6 @@ kj::Maybe<DataUrl> DataUrl::tryParse(kj::StringPtr url) {
return kj::none;
}

static constexpr kj::FixedArray<uint8_t, 256> ascii_whitespace_table = []() consteval {
kj::FixedArray<uint8_t, 256> result{};
for (uint8_t c: {0x09, 0x0a, 0x0c, 0x0d, 0x20}) {
result[c] = true;
}
return result;
}();

constexpr bool isAsciiWhitespace(uint8_t c) noexcept {
return ascii_whitespace_table[c];
}

kj::Maybe<DataUrl> DataUrl::from(const jsg::Url& url) {
if (url.getProtocol() != "data:"_kj) return kj::none;
auto clone = url.clone(jsg::Url::EquivalenceOption::IGNORE_FRAGMENTS);
Expand Down
16 changes: 12 additions & 4 deletions src/workerd/api/encoding.c++
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,18 @@

namespace workerd::api {

constexpr kj::FixedArray<uint8_t, 256> ascii_whitespace_table = []() consteval {
kj::FixedArray<uint8_t, 256> result{};
for (uint8_t c: {0x09, 0x0a, 0x0c, 0x0d, 0x20}) {
result[c] = true;
}
return result;
}();

bool isAsciiWhitespace(uint8_t c) noexcept {
return ascii_whitespace_table[c];
}

// =======================================================================================
// TextDecoder implementation

Expand Down Expand Up @@ -265,10 +277,6 @@ kj::StringPtr getEncodingId(Encoding encoding) {
Encoding getEncodingForLabel(kj::StringPtr label) {
kj::String labelInsensitive = toLower(label);
const auto trim = [](kj::StringPtr label) {
const auto isAsciiWhitespace = [](auto c) {
return c == 0x09 /* tab */ || c == 0x0a /* lf */ || c == 0x0c /* ff */ ||
c == 0x0d /* cr */ || c == 0x20 /* sp */;
};
size_t start = 0;
auto end = label.size();
while (start < end && isAsciiWhitespace(label[start])) {
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/api/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace workerd::api {

bool isAsciiWhitespace(uint8_t c) noexcept;

// The encodings listed here are defined as required by the Encoding spec.
// The first label is enum we use to identify the encoding in code, while
// the second label is the public identifier.
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/api/form-data.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@

#include "form-data.h"

#include "util-iocontext.h"
#include "util.h"

#include <workerd/util/mimetype.h>

#include <kj/compat/http.h>
#include <kj/encoding.h>
#include <kj/parse/char.h>
#include <kj/vector.h>

Expand Down
2 changes: 0 additions & 2 deletions src/workerd/api/gpu/gpu.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@

#include "gpu.h"

#include <workerd/jsg/exception.h>

#include <dawn/dawn_proc.h>

namespace workerd::api::gpu {
Expand Down
2 changes: 0 additions & 2 deletions src/workerd/api/node/async-hooks.c++
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
// https://opensource.org/licenses/Apache-2.0
#include "async-hooks.h"

#include <kj/vector.h>

namespace workerd::api::node {

jsg::Ref<AsyncLocalStorage> AsyncLocalStorage::constructor(jsg::Lock& js) {
Expand Down
3 changes: 2 additions & 1 deletion src/workerd/api/streams/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

#pragma once

#include "../encoding.h"
#include "readable.h"
#include "transform.h"
#include "writable.h"

#include <workerd/api/encoding.h>

namespace workerd::api {

class TextEncoderStream: public TransformStream {
Expand Down
20 changes: 20 additions & 0 deletions src/workerd/api/util-iocontext.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

#include "util-iocontext.h"

#include <workerd/io/io-context.h>

namespace workerd::api {

double dateNow() {
if (IoContext::hasCurrent()) {
auto& ioContext = IoContext::current();
return (ioContext.now() - kj::UNIX_EPOCH) / kj::MILLISECONDS;
}

return 0.0;
}

} // namespace workerd::api
12 changes: 12 additions & 0 deletions src/workerd/api/util-iocontext.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// Copyright (c) 2017-2022 Cloudflare, Inc.
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
// https://opensource.org/licenses/Apache-2.0

#pragma once

namespace workerd::api {

// Returns exactly what Date.now() would return.
double dateNow();

} // namespace workerd::api
11 changes: 0 additions & 11 deletions src/workerd/api/util.c++
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

#include "simdutf.h"

#include <workerd/io/io-context.h>
#include <workerd/util/mimetype.h>
#include <workerd/util/thread-scopes.h>

#include <kj/encoding.h>

Expand Down Expand Up @@ -259,15 +257,6 @@ kj::String redactUrl(kj::StringPtr url) {
return kj::String(redacted.releaseAsArray());
}

double dateNow() {
if (IoContext::hasCurrent()) {
auto& ioContext = IoContext::current();
return (ioContext.now() - kj::UNIX_EPOCH) / kj::MILLISECONDS;
}

return 0.0;
}

kj::Maybe<jsg::V8Ref<v8::Object>> cloneRequestCf(
jsg::Lock& js, kj::Maybe<jsg::V8Ref<v8::Object>> maybeCf) {
KJ_IF_SOME(cf, maybeCf) {
Expand Down
5 changes: 0 additions & 5 deletions src/workerd/api/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,11 +95,6 @@ kj::String redactUrl(kj::StringPtr url);

// =======================================================================================

// Returns exactly what Date.now() would return.
double dateNow();

// =======================================================================================

void maybeWarnIfNotText(jsg::Lock& js, kj::StringPtr str);

kj::String fastEncodeBase64Url(kj::ArrayPtr<const byte> bytes);
Expand Down
9 changes: 8 additions & 1 deletion src/workerd/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ wd_cc_library(
"//src/workerd/api:analytics-engine_capnp",
"//src/workerd/api:data-url",
"//src/workerd/api:deferred-proxy",
"//src/workerd/api:encoding",
"//src/workerd/api:r2-api_capnp",
"//src/workerd/jsg",
"//src/workerd/util:autogate",
Expand Down Expand Up @@ -332,13 +333,19 @@ kj_test(

kj_test(
src = "promise-wrapper-test.c++",
deps = [":io"],
deps = [
":io",
"//src/workerd/api:encoding",
"//src/workerd/api:util-iocontext",
],
)

kj_test(
src = "compatibility-date-test.c++",
deps = [
":io",
"//src/workerd/api:encoding",
"//src/workerd/api:util-iocontext",
"@capnp-cpp//src/capnp:capnpc",
],
)
Expand Down
2 changes: 1 addition & 1 deletion src/workerd/io/io-channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

#pragma once

#include <workerd/api/util.h>
#include <workerd/api/util-iocontext.h>
#include <workerd/io/actor-id.h>
#include <workerd/io/trace.h>

Expand Down
3 changes: 3 additions & 0 deletions src/workerd/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,9 @@ wd_cc_library(
"//src/workerd/io:set_enable_experimental_webgpu": ["WORKERD_EXPERIMENTAL_ENABLE_WEBGPU"],
"//conditions:default": [],
}),
implementation_deps = [
"//src/workerd/api:util-iocontext",
],
visibility = ["//visibility:public"],
deps = [
":actor-id-impl",
Expand Down

0 comments on commit b2ed219

Please sign in to comment.