Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
2 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <algorithm>
#include <cctype>
#include <string>
#include <string_view>
#include <vector>
 
#include "core/common/common.h"
#include "core/common/inlined_containers.h"
 
namespace onnxruntime {
namespace utils {
 
/**
 * Splits a string into substrings delimited by the given delimiter string.
 * @param string_to_split The string to split.
 * @param delimiter The delimiter string.
 * @param keep_empty Whether to keep empty substrings.
 * @return The split substrings.
 */
inline InlinedVector<std::string_view> SplitString(std::string_view string_to_split, std::string_view delimiter,
                                                   bool keep_empty = false) {
  ORT_ENFORCE(!delimiter.empty(), "delimiter must not be empty");
  InlinedVector<std::string_view> result{};
  std::string_view::size_type segment_begin_pos = 0;
  while (segment_begin_pos != std::string_view::npos) {
    const std::string_view::size_type segment_end_pos = string_to_split.find(delimiter, segment_begin_pos);
    const bool is_segment_empty = segment_begin_pos == segment_end_pos || segment_begin_pos == string_to_split.size();
    if (!is_segment_empty || keep_empty) {
      result.push_back(string_to_split.substr(segment_begin_pos, segment_end_pos - segment_begin_pos));
    }
    segment_begin_pos = (segment_end_pos == std::string_view::npos)
                            ? segment_end_pos
                            : segment_end_pos + delimiter.size();
  }
  return result;
}
 
/**
 * Trim a string from start inplace.
 * @param s The string to trim.
 */
inline void TrimStringFromLeft(std::string& s) {
  s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); }));
}
 
/**
 * Trim a string from end inplace.
 * @param s The string to trim.
 */
inline void TrimStringFromRight(std::string& s) {
  s.erase(std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), s.end());
}
 
/**
 * Trim a string from both ends.
 * @param s The string to trim.
 * @return The trimmed string.
 */
inline std::string TrimString(std::string_view s) {
  std::string s_trimmed{s};
  TrimStringFromRight(s_trimmed);
  TrimStringFromLeft(s_trimmed);
  return s_trimmed;
}
 
/**
 * @brief A consistent way to construct the full qualified op name.
 */
inline std::string GetFullQualifiedOpName(const std::string& op_type, const std::string& domain) {
  return MakeString(domain, "::", op_type);
}
 
/**
 * Use this simple hash to generate unique int by given string input.
 */
inline uint32_t GetHashFromString(const std::string& str_value) {
  uint32_t hash = 0;
  for (char const& c : str_value) {
    hash = hash * 101 + c;
  }
 
  return hash;
}
 
/**
 * Returns a lowercase version of the input string.
 * @param str The string to lowercase.
 * @return The lowercased string.
 */
inline std::string GetLowercaseString(std::string str) {
  // https://en.cppreference.com/w/cpp/string/byte/tolower
  // The behavior of tolower from <cctype> is undefined if the argument is neither representable as unsigned char
  // nor equal to EOF. To use tolower safely with a plain char (or signed char), the argument must be converted to
  // unsigned char.
  std::transform(str.begin(), str.end(), str.begin(), [](unsigned char c) {
    return static_cast<char>(std::tolower(c));
  });
  return str;
}
 
}  // namespace utils
}  // namespace onnxruntime