diff --git a/libconvert.cpp b/libconvert.cpp index 3a592a9..46fac85 100644 --- a/libconvert.cpp +++ b/libconvert.cpp @@ -11,7 +11,7 @@ std::string convert::readStringFromFile(const std::string& filename) FILE* fp = fopen(filename.c_str(), "rb"); if (!fp) { - fprintf(stderr, "Can not open file %s\n", filename.c_str()); + fprintf(stderr, "Cannot open file %s\n", filename.c_str()); return ""; } fseek(fp, 0, SEEK_END); @@ -39,7 +39,7 @@ void convert::writeStringAppendToFile(const std::string& str, FILE* fp) fputc('\n', fp); } -int convert::replaceString(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0/*=0*/) +int convert::replaceString(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0 /*=0*/) { int pos = s.find(oldstring, pos0); if (pos >= 0) @@ -65,7 +65,10 @@ int convert::replaceAllString(std::string& s, const std::string& oldstring, cons void convert::replaceStringInFile(const std::string& oldfilename, const std::string& newfilename, const std::string& oldstring, const std::string& newstring) { std::string s = readStringFromFile(oldfilename); - if (s.length() <= 0) { return; } + if (s.length() <= 0) + { + return; + } replaceString(s, oldstring, newstring); writeStringToFile(s, newfilename); } @@ -73,7 +76,10 @@ void convert::replaceStringInFile(const std::string& oldfilename, const std::str void convert::replaceAllStringInFile(const std::string& oldfilename, const std::string& newfilename, const std::string& oldstring, const std::string& newstring) { std::string s = readStringFromFile(oldfilename); - if (s.length() <= 0) { return; } + if (s.length() <= 0) + { + return; + } replaceAllString(s, oldstring, newstring); writeStringToFile(s, newfilename); } @@ -162,14 +168,17 @@ std::vector convert::splitString(std::string str, std::string patte { pattern = ",;| "; } - str += pattern[0]; //扩展字符串以方便操作 + str += pattern[0]; //扩展字符串以方便操作 bool have_space = pattern.find(" ") != std::string::npos; int size = str.size(); for (int i = 0; i < size; i++) { if (have_space) { - while (str[i] == ' ') { i++; } //当空格作为分隔符时,连续空格视为一个 + while (str[i] == ' ') + { + i++; + } //当空格作为分隔符时,连续空格视为一个 } pos = str.find_first_of(pattern, i); if (pos < size) @@ -195,4 +204,3 @@ bool convert::isProChar(char c) { return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'z') || (c >= '(' && c <= ')'); } - diff --git a/libconvert.h b/libconvert.h index 89cde1e..46b4383 100644 --- a/libconvert.h +++ b/libconvert.h @@ -1,15 +1,15 @@ #pragma once -#include -#include #include #include +#include +#include namespace convert { //string functions std::string readStringFromFile(const std::string& filename); void writeStringToFile(const std::string& str, const std::string& filename); -void writeStringAppendToFile(const std::string& str, FILE *fp); +void writeStringAppendToFile(const std::string& str, FILE* fp); int replaceString(std::string& s, const std::string& oldstring, const std::string& newstring, int pos0 = 0); int replaceAllString(std::string& s, const std::string& oldstring, const std::string& newstring); void replaceStringInFile(const std::string& oldfilename, const std::string& newfilename, const std::string& oldstring, const std::string& newstring); @@ -21,7 +21,7 @@ unsigned findTheLast(const std::string& s, const std::string& content); std::vector splitString(std::string str, std::string pattern = "", bool ignore_psspace = true); bool isProChar(char c); -template +template int findNumbers(const std::string& s, std::vector* data) { int n = 0; @@ -35,7 +35,9 @@ int findNumbers(const std::string& s, std::vector* data) { str += c; if (c >= '0' && c <= '9') - { haveNum = true; } + { + haveNum = true; + } } if (!findNumChar || i == s.length() - 1) { @@ -51,4 +53,18 @@ int findNumbers(const std::string& s, std::vector* data) } return n; } + +template +int findNumbers(const std::string& s, std::vector& data) +{ + return findNumbers(s, &data); +} + +template +std::vector findNumbers(const std::string& s) +{ + std::vector data; + findNumbers(s, &data); + return data; +} };