microsoft/onnxruntime-extensions
Publicmirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable
operators/string_utils.h
64lines · modeblame
c891e5d7Wenbing Li5 years ago | 1 | // Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | // Licensed under the MIT License. | |
| 3 | #pragma once | |
| 4 | #include <iostream> | |
| 5 | #include <sstream> | |
| 6 | #include <vector> | |
| 7 | | |
| 8 | template <typename T> | |
| 9 | inline void MakeStringInternal(std::ostringstream& ss, const T& t) noexcept { | |
| 10 | ss << t; | |
| 11 | } | |
| 12 | | |
| 13 | template <> | |
| 14 | inline void MakeStringInternal(std::ostringstream& ss, const std::vector<int64_t>& t) noexcept { | |
| 15 | ss << "["; | |
| 16 | for (int i = 0; i < t.size(); i++) { | |
| 17 | if (i != 0) { | |
| 18 | ss << ", "; | |
| 19 | } | |
| 20 | ss << t[i]; | |
| 21 | } | |
| 22 | ss << "]"; | |
| 23 | } | |
| 24 | | |
| 25 | template <> | |
| 26 | inline void MakeStringInternal(std::ostringstream& ss, const std::vector<std::string>& t) noexcept { | |
| 27 | ss << "["; | |
| 28 | for (int i = 0; i < t.size(); i++) { | |
| 29 | if (i != 0) { | |
| 30 | ss << ", "; | |
| 31 | } | |
| 32 | ss << t[i]; | |
| 33 | } | |
| 34 | ss << "]"; | |
| 35 | } | |
| 36 | | |
| 37 | template <typename T, typename... Args> | |
| 38 | void MakeStringInternal(std::ostringstream& ss, const T& t, const Args&... args) noexcept { | |
| 39 | MakeStringInternal(ss, t); | |
| 40 | MakeStringInternal(ss, args...); | |
| 41 | } | |
| 42 | | |
| 43 | template <typename... Args> | |
| 44 | std::string MakeString(const Args&... args) { | |
| 45 | std::ostringstream ss; | |
| 46 | MakeStringInternal(ss, args...); | |
| 47 | return std::string(ss.str()); | |
| 48 | } | |
| 49 | | |
| 50 | std::vector<std::string_view> SplitString(const std::string_view& str, const std::string_view& seps, bool remove_empty_entries = false); | |
| 51 | | |
aef5ef1eMojimi4 years ago | 52 | bool IsCJK(char32_t c); |
| 53 | | |
| 54 | bool IsAccent(char32_t c); | |
| 55 | | |
| 56 | char32_t StripAccent(char32_t c); | |
c891e5d7Wenbing Li5 years ago | 57 | |
| 58 | uint64_t Hash64(const char* data, size_t n, uint64_t seed); | |
| 59 | | |
| 60 | inline uint64_t Hash64(const char* data, size_t n) { | |
| 61 | return Hash64(data, n, 0xDECAFCAFFE); | |
| 62 | } | |
| 63 | | |
| 64 | uint64_t Hash64Fast(const char* data, size_t n); |