Spicy
string.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <string_view>
7 
8 #include <hilti/rt/extension-points.h>
9 #include <hilti/rt/util.h>
10 
11 namespace hilti::rt {
12 
13 namespace string {
14 
15 /* When processing UTF8, how to handle invalid data not representing UTF8 codepoints. */
16 HILTI_RT_ENUM_WITH_DEFAULT(DecodeErrorStrategy, IGNORE,
17  IGNORE, // skip data
18  REPLACE, // replace with a place-holder
19  STRICT // throw a runtime error
20 );
21 
30 size_t size(const std::string& s, DecodeErrorStrategy errors = DecodeErrorStrategy::REPLACE);
31 
40 std::string lower(const std::string& s, DecodeErrorStrategy errors = DecodeErrorStrategy::REPLACE);
41 
50 std::string upper(const std::string& s, DecodeErrorStrategy errors = DecodeErrorStrategy::REPLACE);
51 
52 } // namespace string
53 
54 namespace detail::adl {
55 inline std::string to_string(const std::string& x, adl::tag /*unused*/) {
56  return fmt("\"%s\"", escapeUTF8(x, true, true, true));
57 }
58 
59 inline std::string to_string(std::string_view x, adl::tag /*unused*/) {
60  return fmt("\"%s\"", escapeUTF8(x, true, true, true));
61 }
62 
63 template<typename CharT, size_t N>
64 inline std::string to_string(const CharT (&x)[N], adl::tag /*unused*/) {
65  return fmt("\"%s\"", escapeUTF8(x, true, true, true));
66 }
67 
68 } // namespace detail::adl
69 
70 template<>
71 inline std::string detail::to_string_for_print<std::string>(const std::string& x) {
72  return escapeUTF8(x, false, false, true);
73 }
74 
75 template<>
76 inline std::string detail::to_string_for_print<std::string_view>(const std::string_view& x) {
77  return escapeUTF8(x, false, false, true);
78 }
79 
80 
81 } // namespace hilti::rt
std::string to_string(T &&x)
Definition: extension-points.h:26
Definition: any.h:7
std::string fmt(const char *fmt, const Args &... args)
Definition: fmt.h:13