Skip to content

Commit

Permalink
Merge pull request #2803 from cloudflare/yagiz/merge-is-ascii-whitespace
Browse files Browse the repository at this point in the history
merge isAsciiWhitespace implementations
  • Loading branch information
anonrig authored Sep 26, 2024
2 parents 60fda54 + 413d5a5 commit bde0b2c
Show file tree
Hide file tree
Showing 17 changed files with 97 additions and 42 deletions.
37 changes: 37 additions & 0 deletions src/workerd/api/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ filegroup(
exclude = [
"html-rewriter.c++",
"data-url.c++",
"encoding.c++",
"util.c++",
"rtti.c++",
"**/*test*.c++",
"pyodide.c++",
Expand All @@ -23,6 +25,8 @@ filegroup(
exclude = [
"html-rewriter.c++",
"data-url.c++",
"encoding.c++",
"util.c++",
"rtti.c++",
"**/*test*.c++",
"pyodide.c++",
Expand All @@ -38,6 +42,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 @@ -119,10 +125,40 @@ kj_test(
],
)

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

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 @@ -204,6 +240,7 @@ kj_test(
kj_test(
src = "api-rtti-test.c++",
deps = [
":encoding",
":html-rewriter",
"//src/workerd/io",
"//src/workerd/jsg:rtti",
Expand Down
1 change: 1 addition & 0 deletions src/workerd/api/analytics-engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <workerd/api/analytics-engine-impl.h>
#include <workerd/api/analytics-engine.capnp.h>
#include <workerd/api/util.h>
#include <workerd/io/io-util.h>
#include <workerd/jsg/jsg.h>

namespace workerd::api {
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
4 changes: 0 additions & 4 deletions src/workerd/api/encoding.c++
Original file line number Diff line number Diff line change
Expand Up @@ -265,10 +265,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
12 changes: 12 additions & 0 deletions src/workerd/api/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,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;
}();

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

// 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 @@ -6,10 +6,10 @@

#include "util.h"

#include <workerd/io/io-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
3 changes: 2 additions & 1 deletion src/workerd/api/memory-cache.c++
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <workerd/api/util.h>
#include <workerd/io/io-context.h>
#include <workerd/io/io-util.h>
#include <workerd/jsg/jsg.h>
#include <workerd/jsg/ser.h>
#include <workerd/util/weak-refs.h>
Expand Down Expand Up @@ -29,7 +30,7 @@ static bool hasExpired(const kj::Maybe<double>& expiration, bool allowOutsideIoC
KJ_IF_SOME(e, expiration) {
double now = (allowOutsideIoContext && !IoContext::hasCurrent())
? getCurrentTimeOutsideIoContext()
: api::dateNow();
: dateNow();
return e < now;
}
return false;
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
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
3 changes: 3 additions & 0 deletions src/workerd/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ wd_cc_library(
"hibernation-manager.c++",
"io-context.c++",
"io-own.c++",
"io-util.c++",
"worker.c++",
] + ["//src/workerd/api:srcs"],
hdrs = [
Expand All @@ -57,6 +58,7 @@ wd_cc_library(
"hibernation-manager.h",
"io-context.h",
"io-own.h",
"io-util.h",
"promise-wrapper.h",
"worker.h",
] + ["//src/workerd/api:hdrs"],
Expand Down Expand Up @@ -89,6 +91,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
4 changes: 2 additions & 2 deletions src/workerd/io/io-channels.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#pragma once

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

#include <kj/debug.h>
Expand Down Expand Up @@ -101,7 +101,7 @@ class IoChannelFactory {
kj::Maybe<kj::StringPtr> featureFlagsForFl;

// Timestamp for when a subrequest is started. (ms since the Unix Epoch)
double startTime = api::dateNow();
double startTime = dateNow();
};

virtual kj::Own<WorkerInterface> startSubrequest(uint channel, SubrequestMetadata metadata) = 0;
Expand Down
22 changes: 22 additions & 0 deletions src/workerd/io/io-util.c++
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 "io-util.h"

#include "io-context.h"

#include <kj/time.h>

namespace workerd {

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

return 0.0;
}

} // namespace workerd
12 changes: 12 additions & 0 deletions src/workerd/io/io-util.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 {

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

} // namespace workerd
2 changes: 1 addition & 1 deletion src/workerd/jsg/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ wd_cc_library(
"web-idl.h",
"wrappable.h",
],
implementation_deps = ["@simdutf"],
visibility = ["//visibility:public"],
deps = [
":exception",
Expand All @@ -64,7 +65,6 @@ wd_cc_library(
"//src/workerd/util:thread-scopes",
"//src/workerd/util:uuid",
"@capnp-cpp//src/kj",
"@simdutf",
"@workerd-v8//:v8",
],
)
Expand Down

0 comments on commit bde0b2c

Please sign in to comment.