xref: /aosp_15_r20/external/pytorch/caffe2/utils/string_utils.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1*da0073e9SAndroid Build Coastguard Worker #pragma once
2*da0073e9SAndroid Build Coastguard Worker 
3*da0073e9SAndroid Build Coastguard Worker #include <algorithm>
4*da0073e9SAndroid Build Coastguard Worker #include <memory>
5*da0073e9SAndroid Build Coastguard Worker #include <string>
6*da0073e9SAndroid Build Coastguard Worker #include <vector>
7*da0073e9SAndroid Build Coastguard Worker 
8*da0073e9SAndroid Build Coastguard Worker #include <c10/macros/Export.h>
9*da0073e9SAndroid Build Coastguard Worker 
10*da0073e9SAndroid Build Coastguard Worker namespace caffe2 {
11*da0073e9SAndroid Build Coastguard Worker 
12*da0073e9SAndroid Build Coastguard Worker TORCH_API std::vector<std::string>
13*da0073e9SAndroid Build Coastguard Worker split(char separator, const std::string& string, bool ignore_empty = false);
14*da0073e9SAndroid Build Coastguard Worker 
15*da0073e9SAndroid Build Coastguard Worker TORCH_API std::string trim(const std::string& str);
16*da0073e9SAndroid Build Coastguard Worker 
17*da0073e9SAndroid Build Coastguard Worker TORCH_API size_t editDistance(
18*da0073e9SAndroid Build Coastguard Worker     const std::string& s1,
19*da0073e9SAndroid Build Coastguard Worker     const std::string& s2,
20*da0073e9SAndroid Build Coastguard Worker     size_t max_distance = 0);
21*da0073e9SAndroid Build Coastguard Worker 
StartsWith(const std::string & str,const std::string & prefix)22*da0073e9SAndroid Build Coastguard Worker TORCH_API inline bool StartsWith(
23*da0073e9SAndroid Build Coastguard Worker     const std::string& str,
24*da0073e9SAndroid Build Coastguard Worker     const std::string& prefix) {
25*da0073e9SAndroid Build Coastguard Worker   return str.length() >= prefix.length() &&
26*da0073e9SAndroid Build Coastguard Worker       std::mismatch(prefix.begin(), prefix.end(), str.begin()).first ==
27*da0073e9SAndroid Build Coastguard Worker       prefix.end();
28*da0073e9SAndroid Build Coastguard Worker }
29*da0073e9SAndroid Build Coastguard Worker 
EndsWith(const std::string & full,const std::string & ending)30*da0073e9SAndroid Build Coastguard Worker TORCH_API inline bool EndsWith(
31*da0073e9SAndroid Build Coastguard Worker     const std::string& full,
32*da0073e9SAndroid Build Coastguard Worker     const std::string& ending) {
33*da0073e9SAndroid Build Coastguard Worker   if (full.length() >= ending.length()) {
34*da0073e9SAndroid Build Coastguard Worker     return (
35*da0073e9SAndroid Build Coastguard Worker         0 ==
36*da0073e9SAndroid Build Coastguard Worker         full.compare(full.length() - ending.length(), ending.length(), ending));
37*da0073e9SAndroid Build Coastguard Worker   } else {
38*da0073e9SAndroid Build Coastguard Worker     return false;
39*da0073e9SAndroid Build Coastguard Worker   }
40*da0073e9SAndroid Build Coastguard Worker }
41*da0073e9SAndroid Build Coastguard Worker 
42*da0073e9SAndroid Build Coastguard Worker TORCH_API int32_t editDistanceHelper(
43*da0073e9SAndroid Build Coastguard Worker     const char* s1,
44*da0073e9SAndroid Build Coastguard Worker     size_t s1_len,
45*da0073e9SAndroid Build Coastguard Worker     const char* s2,
46*da0073e9SAndroid Build Coastguard Worker     size_t s2_len,
47*da0073e9SAndroid Build Coastguard Worker     std::vector<size_t>& current,
48*da0073e9SAndroid Build Coastguard Worker     std::vector<size_t>& previous,
49*da0073e9SAndroid Build Coastguard Worker     std::vector<size_t>& previous1,
50*da0073e9SAndroid Build Coastguard Worker     size_t max_distance);
51*da0073e9SAndroid Build Coastguard Worker } // namespace caffe2
52