mirror of
https://github.com/pytorch/pytorch.git
synced 2025-10-20 21:14:14 +08:00
Summary: This is an automatic change generated by the following script: ``` #!/usr/bin/env python3 from subprocess import check_output, check_call import os def get_compiled_files_list(): import json with open("build/compile_commands.json") as f: data = json.load(f) files = [os.path.relpath(node['file']) for node in data] for idx, fname in enumerate(files): if fname.startswith('build/') and fname.endswith('.DEFAULT.cpp'): files[idx] = fname[len('build/'):-len('.DEFAULT.cpp')] return files def run_clang_tidy(fname): check_call(["python3", "tools/clang_tidy.py", "-c", "build", "-x", fname,"-s"]) changes = check_output(["git", "ls-files", "-m"]) if len(changes) == 0: return check_call(["git", "commit","--all", "-m", f"NOLINT stubs for {fname}"]) def main(): git_files = check_output(["git", "ls-files"]).decode("ascii").split("\n") compiled_files = get_compiled_files_list() for idx, fname in enumerate(git_files): if fname not in compiled_files: continue if fname.startswith("caffe2/contrib/aten/"): continue print(f"[{idx}/{len(git_files)}] Processing {fname}") run_clang_tidy(fname) if __name__ == "__main__": main() ``` Pull Request resolved: https://github.com/pytorch/pytorch/pull/56892 Reviewed By: H-Huang Differential Revision: D27991944 Pulled By: malfet fbshipit-source-id: 5415e1eb2c1b34319a4f03024bfaa087007d7179
122 lines
2.9 KiB
C++
122 lines
2.9 KiB
C++
#include "caffe2/utils/string_utils.h"
|
|
|
|
#include <algorithm>
|
|
#include <sstream>
|
|
#include <vector>
|
|
|
|
namespace caffe2 {
|
|
|
|
std::vector<std::string>
|
|
split(char separator, const std::string& string, bool ignore_empty) {
|
|
std::vector<std::string> pieces;
|
|
std::stringstream ss(string);
|
|
std::string item;
|
|
while (getline(ss, item, separator)) {
|
|
if (!ignore_empty || !item.empty()) {
|
|
pieces.push_back(std::move(item));
|
|
}
|
|
}
|
|
return pieces;
|
|
}
|
|
|
|
std::string trim(const std::string& str) {
|
|
size_t left = str.find_first_not_of(' ');
|
|
if (left == std::string::npos) {
|
|
return str;
|
|
}
|
|
size_t right = str.find_last_not_of(' ');
|
|
return str.substr(left, (right - left + 1));
|
|
}
|
|
|
|
size_t editDistance(
|
|
const std::string& s1, const std::string& s2, size_t max_distance)
|
|
{
|
|
std::vector<size_t> current(s1.length() + 1);
|
|
std::vector<size_t> previous(s1.length() + 1);
|
|
std::vector<size_t> previous1(s1.length() + 1);
|
|
|
|
return editDistanceHelper(
|
|
s1.c_str(),
|
|
s1.length(),
|
|
s2.c_str(),
|
|
s2.length(),
|
|
current,
|
|
previous,
|
|
previous1,
|
|
max_distance
|
|
);
|
|
}
|
|
#define NEXT_UNSAFE(s, i, c) { \
|
|
(c)=(uint8_t)(s)[(i)++]; \
|
|
}
|
|
|
|
int32_t editDistanceHelper(const char* s1,
|
|
size_t s1_len,
|
|
const char* s2,
|
|
size_t s2_len,
|
|
std::vector<size_t> ¤t,
|
|
std::vector<size_t> &previous,
|
|
std::vector<size_t> &previous1,
|
|
size_t max_distance) {
|
|
if (max_distance) {
|
|
if (std::max(s1_len, s2_len) - std::min(s1_len, s2_len) > max_distance) {
|
|
return max_distance+1;
|
|
}
|
|
}
|
|
|
|
for (size_t j = 0; j <= s1_len; ++j) {
|
|
current[j] = j;
|
|
}
|
|
|
|
int32_t str2_offset = 0;
|
|
char prev2 = 0;
|
|
for (size_t i = 1; i <= s2_len; ++i) {
|
|
swap(previous1, previous);
|
|
swap(current, previous);
|
|
current[0] = i;
|
|
|
|
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
|
|
char c2 = s2[str2_offset];
|
|
char prev1 = 0;
|
|
int32_t str1_offset = 0;
|
|
|
|
NEXT_UNSAFE(s2, str2_offset, c2);
|
|
|
|
size_t current_min = s1_len;
|
|
for (size_t j = 1; j <= s1_len; ++j) {
|
|
size_t insertion = previous[j] + 1;
|
|
size_t deletion = current[j - 1] + 1;
|
|
size_t substitution = previous[j - 1];
|
|
size_t transposition = insertion;
|
|
// NOLINTNEXTLINE(clang-analyzer-deadcode.DeadStores)
|
|
char c1 = s1[str1_offset];
|
|
|
|
NEXT_UNSAFE(s1, str1_offset, c1);
|
|
|
|
if (c1 != c2) {
|
|
substitution += 1;
|
|
}
|
|
|
|
|
|
if (prev1 == c2 && prev2 == c1 && j > 1 && i > 1) {
|
|
transposition = previous1[j - 2] + 1;
|
|
}
|
|
prev1 = c1;
|
|
|
|
current[j] = std::min(std::min(insertion, deletion),
|
|
std::min(substitution, transposition));
|
|
current_min = std::min(current_min, current[j]);
|
|
}
|
|
|
|
|
|
if (max_distance != 0 && current_min > max_distance) {
|
|
return max_distance+1;
|
|
}
|
|
|
|
prev2 = c2;
|
|
}
|
|
|
|
return current[s1_len];
|
|
}
|
|
} // namespace caffe2
|