Base: apply clang format
This commit is contained in:
@@ -23,39 +23,44 @@
|
||||
|
||||
#include "PreCompiled.h"
|
||||
#ifndef _PreComp_
|
||||
# include <sstream>
|
||||
# include <locale>
|
||||
# include <iostream>
|
||||
# include <QElapsedTimer>
|
||||
#include <sstream>
|
||||
#include <locale>
|
||||
#include <iostream>
|
||||
#include <QElapsedTimer>
|
||||
#endif
|
||||
|
||||
#include "PyExport.h"
|
||||
#include "Interpreter.h"
|
||||
#include "Tools.h"
|
||||
|
||||
namespace Base {
|
||||
namespace Base
|
||||
{
|
||||
struct string_comp
|
||||
{
|
||||
// s1 and s2 must be numbers represented as string
|
||||
bool operator()(const std::string& s1, const std::string& s2)
|
||||
{
|
||||
if (s1.size() < s2.size())
|
||||
if (s1.size() < s2.size()) {
|
||||
return true;
|
||||
else if (s1.size() > s2.size())
|
||||
}
|
||||
else if (s1.size() > s2.size()) {
|
||||
return false;
|
||||
else
|
||||
}
|
||||
else {
|
||||
return s1 < s2;
|
||||
}
|
||||
}
|
||||
static std::string increment(const std::string& s)
|
||||
{
|
||||
std::string n = s;
|
||||
int addcarry=1;
|
||||
int addcarry = 1;
|
||||
for (std::string::reverse_iterator it = n.rbegin(); it != n.rend(); ++it) {
|
||||
if (addcarry == 0)
|
||||
if (addcarry == 0) {
|
||||
break;
|
||||
}
|
||||
int d = *it - 48;
|
||||
d = d + addcarry;
|
||||
*it = ((d%10) + 48);
|
||||
*it = ((d % 10) + 48);
|
||||
addcarry = d / 10;
|
||||
}
|
||||
if (addcarry > 0) {
|
||||
@@ -73,8 +78,8 @@ class unique_name
|
||||
{
|
||||
public:
|
||||
unique_name(const std::string& name, const std::vector<std::string>& names, int padding)
|
||||
: base_name{name}
|
||||
, padding{padding}
|
||||
: base_name {name}
|
||||
, padding {padding}
|
||||
{
|
||||
removeDigitsFromEnd();
|
||||
findHighestSuffix(names);
|
||||
@@ -89,7 +94,7 @@ private:
|
||||
void removeDigitsFromEnd()
|
||||
{
|
||||
std::string::size_type pos = base_name.find_last_not_of("0123456789");
|
||||
if (pos != std::string::npos && (pos +1) < base_name.size()) {
|
||||
if (pos != std::string::npos && (pos + 1) < base_name.size()) {
|
||||
num_suffix = base_name.substr(pos + 1);
|
||||
base_name.erase(pos + 1);
|
||||
}
|
||||
@@ -97,8 +102,8 @@ private:
|
||||
|
||||
void findHighestSuffix(const std::vector<std::string>& names)
|
||||
{
|
||||
for (const auto & name : names) {
|
||||
if (name.substr(0, base_name.length()) == base_name) { // same prefix
|
||||
for (const auto& name : names) {
|
||||
if (name.substr(0, base_name.length()) == base_name) { // same prefix
|
||||
std::string suffix(name.substr(base_name.length()));
|
||||
if (suffix.size() > 0) {
|
||||
std::string::size_type pos = suffix.find_first_not_of("0123456789");
|
||||
@@ -128,9 +133,10 @@ private:
|
||||
int padding;
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace Base
|
||||
|
||||
std::string Base::Tools::getUniqueName(const std::string& name, const std::vector<std::string>& names, int pad)
|
||||
std::string
|
||||
Base::Tools::getUniqueName(const std::string& name, const std::vector<std::string>& names, int pad)
|
||||
{
|
||||
if (names.empty()) {
|
||||
return name;
|
||||
@@ -154,18 +160,21 @@ std::string Base::Tools::addNumber(const std::string& name, unsigned int num, in
|
||||
|
||||
std::string Base::Tools::getIdentifier(const std::string& name)
|
||||
{
|
||||
if (name.empty())
|
||||
if (name.empty()) {
|
||||
return "_";
|
||||
}
|
||||
// check for first character whether it's a digit
|
||||
std::string CleanName = name;
|
||||
if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57)
|
||||
if (!CleanName.empty() && CleanName[0] >= 48 && CleanName[0] <= 57) {
|
||||
CleanName[0] = '_';
|
||||
}
|
||||
// strip illegal chars
|
||||
for (char & it : CleanName) {
|
||||
if (!((it>=48 && it<=57) || // number
|
||||
(it>=65 && it<=90) || // uppercase letter
|
||||
(it>=97 && it<=122))) // lowercase letter
|
||||
it = '_'; // it's neither number nor letter
|
||||
for (char& it : CleanName) {
|
||||
if (!((it >= 48 && it <= 57) || // number
|
||||
(it >= 65 && it <= 90) || // uppercase letter
|
||||
(it >= 97 && it <= 122))) { // lowercase letter
|
||||
it = '_'; // it's neither number nor letter
|
||||
}
|
||||
}
|
||||
|
||||
return CleanName;
|
||||
@@ -174,29 +183,32 @@ std::string Base::Tools::getIdentifier(const std::string& name)
|
||||
std::wstring Base::Tools::widen(const std::string& str)
|
||||
{
|
||||
std::wostringstream wstm;
|
||||
const std::ctype<wchar_t>& ctfacet = std::use_facet< std::ctype<wchar_t> >(wstm.getloc());
|
||||
for (char i : str)
|
||||
const std::ctype<wchar_t>& ctfacet = std::use_facet<std::ctype<wchar_t>>(wstm.getloc());
|
||||
for (char i : str) {
|
||||
wstm << ctfacet.widen(i);
|
||||
}
|
||||
return wstm.str();
|
||||
}
|
||||
|
||||
std::string Base::Tools::narrow(const std::wstring& str)
|
||||
{
|
||||
std::ostringstream stm;
|
||||
const std::ctype<char>& ctfacet = std::use_facet< std::ctype<char> >(stm.getloc());
|
||||
for (wchar_t i : str)
|
||||
const std::ctype<char>& ctfacet = std::use_facet<std::ctype<char>>(stm.getloc());
|
||||
for (wchar_t i : str) {
|
||||
stm << ctfacet.narrow(i, 0);
|
||||
}
|
||||
return stm.str();
|
||||
}
|
||||
|
||||
std::string Base::Tools::escapedUnicodeFromUtf8(const char *s)
|
||||
std::string Base::Tools::escapedUnicodeFromUtf8(const char* s)
|
||||
{
|
||||
Base::PyGILStateLocker lock;
|
||||
std::string escapedstr;
|
||||
|
||||
PyObject* unicode = PyUnicode_FromString(s);
|
||||
if (!unicode)
|
||||
if (!unicode) {
|
||||
return escapedstr;
|
||||
}
|
||||
|
||||
PyObject* escaped = PyUnicode_AsUnicodeEscapeString(unicode);
|
||||
if (escaped) {
|
||||
@@ -213,9 +225,11 @@ std::string Base::Tools::escapedUnicodeToUtf8(const std::string& s)
|
||||
Base::PyGILStateLocker lock;
|
||||
std::string string;
|
||||
|
||||
PyObject* unicode = PyUnicode_DecodeUnicodeEscape(s.c_str(), static_cast<Py_ssize_t>(s.size()), "strict");
|
||||
if (!unicode)
|
||||
PyObject* unicode =
|
||||
PyUnicode_DecodeUnicodeEscape(s.c_str(), static_cast<Py_ssize_t>(s.size()), "strict");
|
||||
if (!unicode) {
|
||||
return string;
|
||||
}
|
||||
if (PyUnicode_Check(unicode)) {
|
||||
string = PyUnicode_AsUTF8(unicode);
|
||||
}
|
||||
@@ -229,14 +243,18 @@ QString Base::Tools::escapeEncodeString(const QString& s)
|
||||
const int len = s.length();
|
||||
result.reserve(int(len * 1.1));
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (s.at(i) == QLatin1Char('\\'))
|
||||
if (s.at(i) == QLatin1Char('\\')) {
|
||||
result += QLatin1String("\\\\");
|
||||
else if (s.at(i) == QLatin1Char('\"'))
|
||||
}
|
||||
else if (s.at(i) == QLatin1Char('\"')) {
|
||||
result += QLatin1String("\\\"");
|
||||
else if (s.at(i) == QLatin1Char('\''))
|
||||
}
|
||||
else if (s.at(i) == QLatin1Char('\'')) {
|
||||
result += QLatin1String("\\\'");
|
||||
else
|
||||
}
|
||||
else {
|
||||
result += s.at(i);
|
||||
}
|
||||
}
|
||||
result.squeeze();
|
||||
return result;
|
||||
@@ -247,14 +265,18 @@ std::string Base::Tools::escapeEncodeString(const std::string& s)
|
||||
std::string result;
|
||||
size_t len = s.size();
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (s.at(i) == '\\')
|
||||
if (s.at(i) == '\\') {
|
||||
result += "\\\\";
|
||||
else if (s.at(i) == '\"')
|
||||
}
|
||||
else if (s.at(i) == '\"') {
|
||||
result += "\\\"";
|
||||
else if (s.at(i) == '\'')
|
||||
}
|
||||
else if (s.at(i) == '\'') {
|
||||
result += "\\\'";
|
||||
else
|
||||
}
|
||||
else {
|
||||
result += s.at(i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -265,12 +287,15 @@ QString Base::Tools::escapeEncodeFilename(const QString& s)
|
||||
const int len = s.length();
|
||||
result.reserve(int(len * 1.1));
|
||||
for (int i = 0; i < len; ++i) {
|
||||
if (s.at(i) == QLatin1Char('\"'))
|
||||
if (s.at(i) == QLatin1Char('\"')) {
|
||||
result += QLatin1String("\\\"");
|
||||
else if (s.at(i) == QLatin1Char('\''))
|
||||
}
|
||||
else if (s.at(i) == QLatin1Char('\'')) {
|
||||
result += QLatin1String("\\\'");
|
||||
else
|
||||
}
|
||||
else {
|
||||
result += s.at(i);
|
||||
}
|
||||
}
|
||||
result.squeeze();
|
||||
return result;
|
||||
@@ -281,12 +306,15 @@ std::string Base::Tools::escapeEncodeFilename(const std::string& s)
|
||||
std::string result;
|
||||
size_t len = s.size();
|
||||
for (size_t i = 0; i < len; ++i) {
|
||||
if (s.at(i) == '\"')
|
||||
if (s.at(i) == '\"') {
|
||||
result += "\\\"";
|
||||
else if (s.at(i) == '\'')
|
||||
}
|
||||
else if (s.at(i) == '\'') {
|
||||
result += "\\\'";
|
||||
else
|
||||
}
|
||||
else {
|
||||
result += s.at(i);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -305,8 +333,7 @@ std::string Base::Tools::quoted(const std::string& name)
|
||||
return str.str();
|
||||
}
|
||||
|
||||
std::string Base::Tools::joinList(const std::vector<std::string>& vec,
|
||||
const std::string& sep)
|
||||
std::string Base::Tools::joinList(const std::vector<std::string>& vec, const std::string& sep)
|
||||
{
|
||||
std::stringstream str;
|
||||
for (const auto& it : vec) {
|
||||
@@ -324,9 +351,9 @@ struct StopWatch::Private
|
||||
QElapsedTimer t;
|
||||
};
|
||||
|
||||
StopWatch::StopWatch() : d(new Private)
|
||||
{
|
||||
}
|
||||
StopWatch::StopWatch()
|
||||
: d(new Private)
|
||||
{}
|
||||
|
||||
StopWatch::~StopWatch()
|
||||
{
|
||||
@@ -359,13 +386,17 @@ std::string StopWatch::toString(int ms) const
|
||||
int hour = total / 60;
|
||||
std::stringstream str;
|
||||
str << "Needed time: ";
|
||||
if (hour > 0)
|
||||
if (hour > 0) {
|
||||
str << hour << "h " << mins << "m " << secs << "s";
|
||||
else if (mins > 0)
|
||||
}
|
||||
else if (mins > 0) {
|
||||
str << mins << "m " << secs << "s";
|
||||
else if (secs > 0)
|
||||
}
|
||||
else if (secs > 0) {
|
||||
str << secs << "s";
|
||||
else
|
||||
}
|
||||
else {
|
||||
str << msec << "ms";
|
||||
}
|
||||
return str.str();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user