This is an automated email from the ASF dual-hosted git repository. amc pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/master by this push: new 8bb6043 Move the string_view utilities out of TextView into their own files. (#8627) 8bb6043 is described below commit 8bb60439fbf1adfecbbc704c5c86ff7d7050c22b Author: Alan M. Carroll <a...@apache.org> AuthorDate: Sat Jan 22 17:57:14 2022 -0600 Move the string_view utilities out of TextView into their own files. (#8627) --- include/tscpp/util/TextView.h | 75 +------------------------- include/tscpp/util/string_view_util.h | 99 +++++++++++++++++++++++++++++++++++ src/traffic_cache_tool/Makefile.inc | 1 + src/tscpp/util/Makefile.am | 2 +- src/tscpp/util/TextView.cc | 41 --------------- src/tscpp/util/string_view_util.cc | 63 ++++++++++++++++++++++ 6 files changed, 165 insertions(+), 116 deletions(-) diff --git a/include/tscpp/util/TextView.h b/include/tscpp/util/TextView.h index 6d14070..dae73ab 100644 --- a/include/tscpp/util/TextView.h +++ b/include/tscpp/util/TextView.h @@ -34,80 +34,7 @@ #include <string_view> #include <limits> -/** Compare views with ordering, ignoring case. - * - * @param lhs input view - * @param rhs input view - * @return The ordered comparison value. - * - * - -1 if @a lhs is less than @a rhs - * - 1 if @a lhs is greater than @a rhs - * - 0 if the views have identical content. - * - * If one view is the prefix of the other, the shorter view is less (first in the ordering). - */ -int strcasecmp(const std::string_view &lhs, const std::string_view &rhs); - -/** Compare views with ordering. - * - * @param lhs input view - * @param rhs input view - * @return The ordered comparison value. - * - * - -1 if @a lhs is less than @a rhs - * - 1 if @a lhs is greater than @a rhs - * - 0 if the views have identical content. - * - * If one view is the prefix of the other, the shorter view is less (first in the ordering). - * - * @note For string views, there is no difference between @c strcmp and @c memcmp. - * @see strcmp - */ -int memcmp(const std::string_view &lhs, const std::string_view &rhs); - -/** Copy bytes. - * - * @param dst Destination buffer. - * @param src Original string. - * @return @a dest - * - * This is a convenience for - * @code - * memcpy(dst, src.data(), size.size()); - * @endcode - * Therefore @a dst must point at a buffer large enough to hold @a src. If this is not already - * determined, then presuming @c DST_SIZE is the size of the buffer at @a dst - * @code - * memcpy(dst, src.prefix(DST_SIZE)); - * @endcode - * - */ -inline void * -memcpy(void *dst, const std::string_view &src) -{ - return memcpy(dst, src.data(), src.size()); -} - -/** Compare views with ordering. - * - * @param lhs input view - * @param rhs input view - * @return The ordered comparison value. - * - * - -1 if @a lhs is less than @a rhs - * - 1 if @a lhs is greater than @a rhs - * - 0 if the views have identical content. - * - * If one view is the prefix of the other, the shorter view is less (first in the ordering). - * - * @note For string views, there is no difference between @c strcmp and @c memcmp. - * @see memcmp - */ -inline int -strcmp(const std::string_view &lhs, const std::string_view &rhs) -{ - return memcmp(lhs, rhs); -} +#include "tscpp/util/string_view_util.h" namespace ts { diff --git a/include/tscpp/util/string_view_util.h b/include/tscpp/util/string_view_util.h new file mode 100644 index 0000000..5be1cd4 --- /dev/null +++ b/include/tscpp/util/string_view_util.h @@ -0,0 +1,99 @@ +/** @file + + Utility overloads for @c std::string_view + + @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 <string_view> +#include <string.h> +#include <strings.h> + +/** Compare views with ordering, ignoring case. + * + * @param lhs input view + * @param rhs input view + * @return The ordered comparison value. + * + * - -1 if @a lhs is less than @a rhs + * - 1 if @a lhs is greater than @a rhs + * - 0 if the views have identical content. + * + * If one view is the prefix of the other, the shorter view is less (first in the ordering). + */ +int strcasecmp(const std::string_view &lhs, const std::string_view &rhs); + +/** Compare views with ordering. + * + * @param lhs input view + * @param rhs input view + * @return The ordered comparison value. + * + * - -1 if @a lhs is less than @a rhs + * - 1 if @a lhs is greater than @a rhs + * - 0 if the views have identical content. + * + * If one view is the prefix of the other, the shorter view is less (first in the ordering). + * + * @note For string views, there is no difference between @c strcmp and @c memcmp. + * @see strcmp + */ +int memcmp(const std::string_view &lhs, const std::string_view &rhs); + +/** Compare views with ordering. + * + * @param lhs input view + * @param rhs input view + * @return The ordered comparison value. + * + * - -1 if @a lhs is less than @a rhs + * - 1 if @a lhs is greater than @a rhs + * - 0 if the views have identical content. + * + * If one view is the prefix of the other, the shorter view is less (first in the ordering). + * + * @note For string views, there is no difference between @c strcmp and @c memcmp. + * @see memcmp + */ +inline int +strcmp(const std::string_view &lhs, const std::string_view &rhs) +{ + return memcmp(lhs, rhs); +} + +/** Copy bytes. + * + * @param dst Destination buffer. + * @param src Original string. + * @return @a dest + * + * This is a convenience for + * @code + * memcpy(dst, src.data(), size.size()); + * @endcode + * Therefore @a dst must point at a buffer large enough to hold @a src. If this is not already + * determined, then presuming @c DST_SIZE is the size of the buffer at @a dst + * @code + * memcpy(dst, src.prefix(DST_SIZE)); + * @endcode + * + */ +inline void * +memcpy(void *dst, const std::string_view &src) +{ + return memcpy(dst, src.data(), src.size()); +} diff --git a/src/traffic_cache_tool/Makefile.inc b/src/traffic_cache_tool/Makefile.inc index 6af99fa..2d972e3 100644 --- a/src/traffic_cache_tool/Makefile.inc +++ b/src/traffic_cache_tool/Makefile.inc @@ -45,6 +45,7 @@ traffic_cache_tool_traffic_cache_tool_LDADD = \ $(top_builddir)/src/tscore/.libs/ts_file.o \ $(top_builddir)/src/tscore/.libs/Errata.o \ $(top_builddir)/src/tscpp/util/.libs/TextView.o \ + $(top_builddir)/src/tscpp/util/.libs/string_view_util.o \ $(top_builddir)/src/tscore/.libs/Regex.o \ $(top_builddir)/src/tscore/.libs/CryptoHash.o \ $(top_builddir)/src/tscore/.libs/MMH.o \ diff --git a/src/tscpp/util/Makefile.am b/src/tscpp/util/Makefile.am index 40139bf..f87a06b 100644 --- a/src/tscpp/util/Makefile.am +++ b/src/tscpp/util/Makefile.am @@ -29,7 +29,7 @@ AM_CPPFLAGS += -I$(abs_top_srcdir)/include libtscpputil_la_LDFLAGS = @AM_LDFLAGS@ -no-undefined -version-info @TS_LIBTOOL_VERSION@ libtscpputil_la_SOURCES = \ - TextView.cc + TextView.cc string_view_util.cc test_tscpputil_CPPFLAGS = $(AM_CPPFLAGS)\ -I$(abs_top_srcdir)/tests/include diff --git a/src/tscpp/util/TextView.cc b/src/tscpp/util/TextView.cc index d66aeff..c618676 100644 --- a/src/tscpp/util/TextView.cc +++ b/src/tscpp/util/TextView.cc @@ -29,47 +29,6 @@ static_assert(size_t(-1) == std::string_view::npos, "TextView assumes -1 is the same as npos"); -int -memcmp(std::string_view const &lhs, std::string_view const &rhs) -{ - int zret = 0; - size_t n = rhs.size(); - - // Seems a bit ugly but size comparisons must be done anyway to get the memcmp args. - if (lhs.size() < rhs.size()) { - zret = 1; - n = lhs.size(); - } else if (lhs.size() > rhs.size()) { - zret = -1; - } else if (lhs.data() == rhs.data()) { // same memory, obviously equal. - return 0; - } - - int r = ::memcmp(lhs.data(), rhs.data(), n); - return r ? r : zret; -} - -int -strcasecmp(const std::string_view &lhs, const std::string_view &rhs) -{ - int zret = 0; - size_t n = rhs.size(); - - // Seems a bit ugly but size comparisons must be done anyway to get the @c strncasecmp args. - if (lhs.size() < rhs.size()) { - zret = 1; - n = lhs.size(); - } else if (lhs.size() > rhs.size()) { - zret = -1; - } else if (lhs.data() == rhs.data()) { // the same memory, obviously equal. - return 0; - } - - int r = ::strncasecmp(lhs.data(), rhs.data(), n); - - return r ? r : zret; -} - const int8_t ts::svtoi_convert[256] = { /* [can't do this nicely because clang format won't allow extended comments] 0 1 2 3 4 5 6 7 8 9 A B C D E F diff --git a/src/tscpp/util/string_view_util.cc b/src/tscpp/util/string_view_util.cc new file mode 100644 index 0000000..5cc213b --- /dev/null +++ b/src/tscpp/util/string_view_util.cc @@ -0,0 +1,63 @@ +/** @file + + Utilities for @c std::string_view + + @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 "tscpp/util/string_view_util.h" + +int +memcmp(std::string_view const &lhs, std::string_view const &rhs) +{ + int zret = 0; + size_t n = rhs.size(); + + // Seems a bit ugly but size comparisons must be done anyway to get the memcmp args. + if (lhs.size() < rhs.size()) { + zret = 1; + n = lhs.size(); + } else if (lhs.size() > rhs.size()) { + zret = -1; + } else if (lhs.data() == rhs.data()) { // same memory, obviously equal. + return 0; + } + + int r = ::memcmp(lhs.data(), rhs.data(), n); + return r ? r : zret; +} + +int +strcasecmp(const std::string_view &lhs, const std::string_view &rhs) +{ + int zret = 0; + size_t n = rhs.size(); + + // Seems a bit ugly but size comparisons must be done anyway to get the @c strncasecmp args. + if (lhs.size() < rhs.size()) { + zret = 1; + n = lhs.size(); + } else if (lhs.size() > rhs.size()) { + zret = -1; + } else if (lhs.data() == rhs.data()) { // the same memory, obviously equal. + return 0; + } + + int r = ::strncasecmp(lhs.data(), rhs.data(), n); + + return r ? r : zret; +}