summaryrefslogtreecommitdiff
path: root/src/tools
diff options
context:
space:
mode:
authorOleg Morozenkov <m@oleg.rocks>2018-07-23 02:35:50 +0300
committerOleg Morozenkov <m@oleg.rocks>2018-07-23 02:35:50 +0300
commit98b8b7e4338b71ee46c4301b0bf2ae667be9a99d (patch)
tree32f8b0d32048b2d83b57773c0efa3db9600b8701 /src/tools
parent1dd3affe306793d2129f121c11e43c45ae8690da (diff)
parent167e3e7607e43a0f06c7f87ced94f481e6525b0e (diff)
Merge branch 'master' into nicholascw-master
Diffstat (limited to 'src/tools')
-rw-r--r--src/tools/FileTools.cpp30
-rw-r--r--src/tools/StringTools.cpp139
2 files changed, 84 insertions, 85 deletions
diff --git a/src/tools/FileTools.cpp b/src/tools/FileTools.cpp
index 105d80f..279f152 100644
--- a/src/tools/FileTools.cpp
+++ b/src/tools/FileTools.cpp
@@ -12,24 +12,24 @@ using namespace std;
namespace FileTools {
string read(const string& filePath) {
- ifstream in(filePath, ios::in | ios::binary);
- if (!in) {
- throw system_error(errno, system_category());
- }
- ostringstream contents;
- contents << in.rdbuf();
- in.close();
- return contents.str();
+ ifstream in(filePath, ios::in | ios::binary);
+ if (!in) {
+ throw system_error(errno, system_category());
+ }
+ ostringstream contents;
+ contents << in.rdbuf();
+ in.close();
+ return contents.str();
}
bool write(const string& content, const string& filePath) {
- ofstream out(filePath, ios::out | ios::binary);
- if (!out) {
- return false;
- }
- out << content;
- out.close();
- return true;
+ ofstream out(filePath, ios::out | ios::binary);
+ if (!out) {
+ return false;
+ }
+ out << content;
+ out.close();
+ return true;
}
};
diff --git a/src/tools/StringTools.cpp b/src/tools/StringTools.cpp
index 0e68dcc..5055d8e 100644
--- a/src/tools/StringTools.cpp
+++ b/src/tools/StringTools.cpp
@@ -22,100 +22,99 @@
#include "tgbot/tools/StringTools.h"
-#include <stdlib.h>
+#include <cstdlib>
#include <iomanip>
-#include <stdio.h>
+#include <cstdio>
using namespace std;
namespace StringTools {
bool startsWith(const string& str1, const string& str2) {
- if (str1.length() < str2.length()) {
- return false;
- }
- string::const_iterator it1(str1.begin());
- string::const_iterator end1(str1.end());
- string::const_iterator it2(str2.begin());
- string::const_iterator end2(str2.end());
- while (it1 != end1 && it2 != end2) {
- if (*it1 != *it2) {
- return false;
- }
- ++it1;
- ++it2;
- }
- return true;
+ if (str1.length() < str2.length()) {
+ return false;
+ }
+ string::const_iterator it1(str1.begin());
+ string::const_iterator end1(str1.end());
+ string::const_iterator it2(str2.begin());
+ string::const_iterator end2(str2.end());
+ while (it1 != end1 && it2 != end2) {
+ if (*it1 != *it2) {
+ return false;
+ }
+ ++it1;
+ ++it2;
+ }
+ return true;
}
bool endsWith(const string& str1, const string& str2) {
- if (str1.length() < str2.length()) {
- return false;
- }
- string::const_iterator it1(str1.end());
- string::const_iterator begin1(str1.begin());
- string::const_iterator it2(str2.end());
- string::const_iterator begin2(str2.begin());
- --begin1;
- --begin2;
- while (it1 != begin1 && it2 != begin2) {
- if (*it1 != *it2) {
- return false;
- }
- --it1;
- --it2;
- }
- return true;
+ if (str1.length() < str2.length()) {
+ return false;
+ }
+ string::const_iterator it1(str1.end());
+ string::const_iterator begin1(str1.begin());
+ string::const_iterator it2(str2.end());
+ string::const_iterator begin2(str2.begin());
+ --begin1;
+ --begin2;
+ while (it1 != begin1 && it2 != begin2) {
+ if (*it1 != *it2) {
+ return false;
+ }
+ --it1;
+ --it2;
+ }
+ return true;
}
void split(const string& str, char delimiter, vector<string>& dest) {
- istringstream stream(str);
- string s;
- while (getline(stream, s, delimiter)) {
- dest.push_back(s);
- }
+ istringstream stream(str);
+ string s;
+ while (getline(stream, s, delimiter)) {
+ dest.push_back(s);
+ }
}
string generateRandomString(size_t length) {
- static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~");
- static const size_t charsLen = chars.length();
- string result;
- for (size_t i = 0; i < length; ++i) {
- result += chars[rand() % charsLen];
- }
- return result;
+ static const string chars("qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890-=[]\\;',./!@#$%^&*()_+{}|:\"<>?`~");
+ static const size_t charsLen = chars.length();
+ string result;
+ for (size_t i = 0; i < length; ++i) {
+ result += chars[rand() % charsLen];
+ }
+ return result;
}
string urlEncode(const string& value, const std::string& additionalLegitChars) {
- static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:";
- std::stringstream ss;
- for (auto const &c : value) {
- if ((legitPunctuation.find(c) == std::string::npos)
- && (additionalLegitChars.find(c)==std::string::npos)) {
- ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c;
- } else {
- ss << c;
- }
- }
+ static const string legitPunctuation = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_.-~:";
+ std::stringstream ss;
+ for (auto const &c : value) {
+ if ((legitPunctuation.find(c) == std::string::npos) && (additionalLegitChars.find(c) == std::string::npos)) {
+ ss << '%' << std::uppercase << std::setfill('0') << std::setw(2) << std::hex << (unsigned int)(unsigned char)c;
+ } else {
+ ss << c;
+ }
+ }
- return ss.str();
+ return ss.str();
}
string urlDecode(const string& value) {
- string result;
- for (size_t i = 0, count = value.length(); i < count; ++i) {
- const char c = value[i];
- if (c == '%') {
- int t = 0;
- sscanf(value.substr(i + 1, 2).c_str(), "%x", &t);
- result += (char) t;
- i += 2;
- } else {
- result += c;
- }
- }
- return result;
+ string result;
+ for (size_t i = 0, count = value.length(); i < count; ++i) {
+ const char c = value[i];
+ if (c == '%') {
+ int t = 0;
+ sscanf(value.substr(i + 1, 2).c_str(), "%x", &t);
+ result += (char) t;
+ i += 2;
+ } else {
+ result += c;
+ }
+ }
+ return result;
}
}