You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@trafficserver.apache.org by "SolidWallOfCode (via GitHub)" <gi...@apache.org> on 2023/03/03 16:08:25 UTC

[GitHub] [trafficserver] SolidWallOfCode commented on a diff in pull request #9485: Unit parser

SolidWallOfCode commented on code in PR #9485:
URL: https://github.com/apache/trafficserver/pull/9485#discussion_r1124670916


##########
src/tscpp/util/ts_unit_parser.cc:
##########
@@ -0,0 +1,78 @@
+/** @file Support for common diagnostics between core, plugins, and libswoc.
+
+  This enables specifying the set of methods usable by a user agent based on the remove IP address
+  for a user agent connection.
+
+  @section license License
+
+  Licensed to the Apache Software Foundation (ASF) under one
+  or more contributor license agreements.  See the NOTICE file
+  distributed with this work for additional information
+  regarding copyright ownership.  The ASF licenses this file
+  to you under the Apache License, Version 2.0 (the
+  "License"); you may not use this file except in compliance
+  with the License.  You may obtain a copy of the License at
+
+      http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+*/
+
+#include <chrono>
+
+#include "tscpp/util/ts_unit_parser.h"
+
+namespace ts
+{
+
+auto
+UnitParser::operator()(swoc::TextView const &src) const noexcept -> Rv<value_type>
+{
+  value_type zret = 0;
+  TextView text   = src; // Keep @a src around to report error offsets.
+
+  while (text.ltrim_if(&isspace)) {
+    TextView parsed;
+    auto n = swoc::svtou(text, &parsed);
+    if (parsed.empty()) {
+      return Errata("Required count not found at offset {}", text.data() - src.data());
+    } else if (n == std::numeric_limits<decltype(n)>::max()) {
+      return Errata("Count at offset {} was out of bounds", text.data() - src.data());
+    }
+    text.remove_prefix(parsed.size());
+    auto ptr = text.ltrim_if(&isspace).data(); // save for error reporting.
+    // Everything up to the next digit or whitespace.
+    auto unit = text.clip_prefix_of([](char c) { return !(isspace(c) || isdigit(c)); });
+    if (unit.empty()) {
+      if (_unit_required_p) {
+        return Errata("Required unit not found at offset {}", ptr - src.data());
+      }
+    } else {
+      auto mult = _units[unit]; // What's the multiplier?
+      if (mult == 0) {
+        return Errata("Unknown unit \"{}\" at offset {}", unit, ptr - src.data());
+      }
+      n *= mult;
+    }
+    zret += n;
+  }
+  return zret;
+}
+
+// Concrete unit parsers.
+
+using namespace std::chrono;
+UnitParser const time_parser{UnitParser::Units{{{nanoseconds{1}.count(), {"ns", "nanosec", "nanoseconds"}},
+                                                {nanoseconds{microseconds{1}}.count(), {"us", "microsec", "microseconds"}},
+                                                {nanoseconds{milliseconds{1}}.count(), {"ms", "millisec", "milliseconds"}},
+                                                {nanoseconds{seconds{1}}.count(), {"s", "sec", "seconds"}},
+                                                {nanoseconds{minutes{1}}.count(), {"m", "min", "minutes"}},
+                                                {nanoseconds{hours{1}}.count(), {"h", "hour", "hours"}},
+                                                {nanoseconds{hours{24}}.count(), {"d", "day", "days"}},
+                                                {nanoseconds{hours{168}}.count(), {"w", "week", "weeks"}}}}};
+
+} // namespace ts

Review Comment:
   No, there's still computation that occurs to initialize the parser data structure. I'm not sure what you mean by "static initializer" other than the parser being a static data structure which is constructed at process start time. That is,
   ```
   UnitParser const time_parser{ ... };
   ```
   which declares a static instance of `UnitParser`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@trafficserver.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org