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

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

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


##########
include/tscpp/util/ts_unit_parser.h:
##########
@@ -0,0 +1,103 @@
+/** @file
+
+  Parse strings with units.
+
+  @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.
+*/
+
+#pragma once
+
+#include <cstdint>
+
+#include "swoc/Lexicon.h"
+#include "swoc/Errata.h"
+
+namespace ts
+{
+
+using swoc::TextView;
+using swoc::Lexicon;
+using swoc::Errata;
+using swoc::Rv;
+
+/** Parse a string that consists of counts and units.
+ *
+ * Give a set of units, each of which is a list of names and a multiplier, parse a string. The
+ * string contents must consist of (optional whitespace) with alternating counts and units,
+ * starting with a count. Each count is multiplied by the value of the subsequent unit. Optionally
+ * the parser can be set to allow counts without units, which are not multiplied.
+ *
+ * For example, if the units were [ "X", 10 ] , [ "L", 50 ] , [ "C", 100 ] , [ "M", 1000 ]
+ * then the following strings would be parsed as
+ *
+ * - "1X" : 10
+ * - "1L3X" : 80
+ * - "2C" : 200
+ * - "1M 4C 4X" : 1,440
+ * - "3M 5 C3 X" : 3,530
+ */
+class UnitParser
+{
+  using self_type = UnitParser; ///< Self reference type.
+public:
+  using value_type = uintmax_t;                 ///< Integral type returned.
+  using Units      = swoc::Lexicon<value_type>; ///< Unit definition type.
+
+  /// Symbolic name for setting whether units are required.
+  static constexpr bool UNITS_REQUIRED = true;
+  /// Symbolic name for setting whether units are required.
+  static constexpr bool UNITS_NOT_REQUIRED = false;
+
+  /** Constructor.
+   *
+   * @param units A @c Lexicon of unit definitions.
+   * @param unit_required_p Whether valid input requires units on all values.
+   */
+  UnitParser(Units &&units, bool unit_required_p = true) noexcept;
+
+  /** Set whether a unit is required.
+   *
+   * @param flag @c true if a unit is required, @c false if not.
+   * @return @a this.
+   */
+  self_type &unit_required(bool flag);
+
+  /** Parse a string.
+   *
+   * @param src Input string.
+   * @return The computed value if the input it valid, or an error report.

Review Comment:
   "it valid" -> "is valid"



-- 
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