/* Copyright (c) 2008-2022 Jan W. Krieger () This software is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License (LGPL) as published by the Free Software Foundation, either version 2.1 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License (LGPL) for more details. You should have received a copy of the GNU Lesser General Public License (LGPL) along with this program. If not, see . */ #include "jkqtcommon/jkqtpstringtools.h" #include "jkqtcommon/jkqtpmathtools.h" #include #include #include #include #include #include #include #include #include #include #include #include #include #if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0)) #include #include #else #include #endif std::string jkqtp_tolower(const std::string& s){ std::string d; d=""; std::locale loc; if (s.length()>0) { for (unsigned long i=0; i0) { for (unsigned long i=0; i=1024.0) res=jkqtp_format(form,data/1024.0)+" k"; if (fabs(data)>=1024.0*1024.0) res=jkqtp_format(form,data/(1024.0*1024.0))+" M"; if (fabs(data)>=1024.0*1024.0*1024.0) res=jkqtp_format(form,data/(1024.0*1024.0*1024.0))+" "; if (fabs(data)>=1024.0*1024.0*1024.0*1024.0) res=jkqtp_format(form,data/(1024.0*1024.0*1024.0*1024.0))+" G"; if (fabs(data)>=1024.0*1024.0*1024.0*1024.0*1024.0) res=jkqtp_format(form,data/(1024.0*1024.0*1024.0*1024.0*1024.0))+" T"; if (fabs(data)>=1024.0*1024.0*1024.0*1024.0*1024.0*1024.0) res=jkqtp_format(form,data/(1024.0*1024.0*1024.0*1024.0*1024.0*1024.0))+" E"; if (fabs(data)==0) res="0 "; return res+"Bytes"; } std::string jkqtp_inttostr(long data){ return jkqtp_format("%ld", data); }; std::string jkqtp_inttohex(long data){ return jkqtp_format("%lX", data); }; std::string jkqtp_uinttostr(unsigned long data){ std::ostringstream ost; ost<0) { if (fabs(data)(r.size())-1; bool nonz=false; while (i>=0) { //std::cout<=1e3) { u="k"; factor=1e3; } if (data>=1e6) { u="M"; factor=1e6; } if (data>=1e9) { u="G"; factor=1e9; } if (data>=1e12) { u="T"; factor=1e12; } if (data>=1e15) { u="P"; factor=1e15; } if (data>=1e18) { u="E"; factor=1e18; } if (data<1) {u="m"; factor=1e-3; } if (data<1e-3) {u="u"; factor=1e-6; } if (data<1e-6) {u="n"; factor=1e-9; } if (data<1e-9) {u="p"; factor=1e-12; } if (data<1e-12) {u="f"; factor=1e-15; } if (data<1e-15) {u="a"; factor=1e-18; } return jkqtp_floattostr(dataa/factor)+u+unitname; }; std::string jkqtp_floattounitstr(double data, int past_comma, bool remove_trail0){ if (data==0) return "0"; std::string form="%."+jkqtp_inttostr(past_comma)+"lf"; std::string res=jkqtp_format(form,data); if (fabs(data)>=1e3) res=jkqtp_format(form,data/1e3)+"k"; if (fabs(data)>=1e6) res=jkqtp_format(form,data/1e6)+"M"; if (fabs(data)>=1e9) res=jkqtp_format(form,data/1e9)+"G"; if (fabs(data)>=1e12) res=jkqtp_format(form,data/1e12)+"T"; if (fabs(data)>=1e15) res=jkqtp_format(form,data/1e15)+"P"; if (fabs(data)<1) res=jkqtp_format(form,data/1e-3)+"m"; if (fabs(data)<1e-3) res=jkqtp_format(form,data/1e-6)+"u"; if (fabs(data)<1e-6) res=jkqtp_format(form,data/1e-9)+"n"; if (fabs(data)<1e-9) res=jkqtp_format(form,data/1e-12)+"f"; if (fabs(data)==0) res=jkqtp_format(form,data); if (remove_trail0) { if (data==0) return "0"; if (res.find('.')==std::string::npos) return res; size_t i=res.size()-1; while (i>0 && res[i]=='0') { i--; } if (res[i]=='.') i--; // remove decimal divider return res.erase(i+1); } return res; } std::string jkqtp_floattolatexstr(double data, int past_comma, bool remove_trail0, double belowIsZero, double minNoExponent, double maxNoExponent, bool ensurePlusMinus){ if ((belowIsZero>0) && (fabs(data)::min()) { if (ensurePlusMinus) return "+\\rm{0}"; else return "\\rm{0}"; } double adata=fabs(data); std::string res=jkqtp_floattostr(data, past_comma, remove_trail0); long exp=static_cast(floor(log(adata)/JKQTPSTATISTICS_LN10)); if ((minNoExponent>fabs(data)) || (fabs(data)>maxNoExponent)) { std::string v=jkqtp_floattostr(data/pow(10.0, static_cast(exp)), past_comma, remove_trail0); if (v!="1" && v!="10") { res=v+std::string("{\\times}10^{")+jkqtp_inttostr(exp)+"}"; } else { if (v=="10") exp=exp+1; res=std::string("10^{")+jkqtp_inttostr(exp)+"}"; } } if (ensurePlusMinus && res.size()>0) { if (res[0]!='-' && res[0]!='+') { if (data<0) res="-"+res; else res="+"+res; } } return res; } std::string jkqtp_floattohtmlstr(double data, int past_comma, bool remove_trail0, double belowIsZero, double minNoExponent, double maxNoExponent){ std::string result; if ((belowIsZero>0) && (fabs(data)::min()) return "0"; double adata=fabs(data); std::string res=jkqtp_floattostr(data, past_comma, remove_trail0); long exp=static_cast(floor(log(adata)/JKQTPSTATISTICS_LN10)); if ((minNoExponent<=fabs(data)) && (fabs(data)(exp)), past_comma, remove_trail0); if (v!="1") result= v+std::string("×10")+jkqtp_inttostr(exp)+""; else result=std::string("10")+jkqtp_inttostr(exp)+""; } //std::cout<<"floattohtmlstr("<(r), 2,16,QLatin1Char('0')).arg(static_cast(g), 2,16,QLatin1Char('0')).arg(static_cast(b), 2,16,QLatin1Char('0')); } // if we reach this, we have an unnamed transparent color if (useSpecialTransparencySyntax) { QString col=jkqtp_rgbtostring(r,g,b,255,false); return QString("%1,%2").arg(col).arg(static_cast(a), 0, 10); //return QString("%1,%2%%").arg(col).arg(static_cast(a)*100/255, 0, 10); } else { return QString("#%1%2%3%4").arg(static_cast(r), 2,16,QLatin1Char('0')).arg(static_cast(g), 2,16,QLatin1Char('0')).arg(static_cast(b), 2,16,QLatin1Char('0')).arg(static_cast(a), 2,16,QLatin1Char('0')); } } QString jkqtp_QColor2String(QColor color, bool useSpecialTransparencySyntax) { return jkqtp_rgbtostring(static_cast((color).red()), static_cast((color).green()), static_cast((color).blue()), static_cast((color).alpha()), useSpecialTransparencySyntax); } QColor jkqtp_lookupQColorName(const QString &color) { const QString col=color.toLower().trimmed(); if (col=="window") return QGuiApplication::palette().color(QPalette::Window); if (col=="windowtext") return QGuiApplication::palette().color(QPalette::WindowText); if (col=="button") return QGuiApplication::palette().color(QPalette::Button); if (col=="light") return QGuiApplication::palette().color(QPalette::Light); if (col=="midlight") return QGuiApplication::palette().color(QPalette::Midlight); if (col=="dark") return QGuiApplication::palette().color(QPalette::Dark); if (col=="mid") return QGuiApplication::palette().color(QPalette::Mid); if (col=="text") return QGuiApplication::palette().color(QPalette::Text); if (col=="brightttext") return QGuiApplication::palette().color(QPalette::BrightText); if (col=="base") return QGuiApplication::palette().color(QPalette::Base); if (col=="window") return QGuiApplication::palette().color(QPalette::Window); if (col=="shadow") return QGuiApplication::palette().color(QPalette::Shadow); if (col=="highlight") return QGuiApplication::palette().color(QPalette::Highlight); if (col=="highlightedtext") return QGuiApplication::palette().color(QPalette::HighlightedText); if (col=="link") return QGuiApplication::palette().color(QPalette::Link); if (col=="linkvisited") return QGuiApplication::palette().color(QPalette::LinkVisited); if (col=="alternatebase") return QGuiApplication::palette().color(QPalette::AlternateBase); if (col=="norole") return QGuiApplication::palette().color(QPalette::NoRole); if (col=="tooltipbase") return QGuiApplication::palette().color(QPalette::ToolTipBase); if (col=="tooltiptext") return QGuiApplication::palette().color(QPalette::ToolTipText); if (col=="placeholdertext") return QGuiApplication::palette().color(QPalette::PlaceholderText); for (int i=0; i=QT_VERSION_CHECK(6, 0, 0)) QRegularExpression rxP("(.+)\\s*,\\s*t?\\s*(\\d+\\.?\\d+)\\%"); QRegularExpression rxAP("(.+)\\s*,\\s*a\\s*(\\d+\\.?\\d+)\\%"); QRegularExpression rxNP("(.+)\\s*,\\s*a?\\s*([\\d]+)"); const auto mP=rxP.match(color); if (mP.hasMatch()) { QColor col=jkqtp_lookupQColorName(mP.captured(1)); double a=QLocale::c().toDouble(mP.captured(2)); col.setAlphaF(1.0-a/100.0); return col; } const auto mAP=rxAP.match(color); if (mAP.hasMatch()) { QColor col=jkqtp_lookupQColorName(mAP.captured(1)); double a=QLocale::c().toDouble(mAP.captured(2)); col.setAlphaF(a/100.0); return col; } const auto mNP=rxNP.match(color); if (mNP.hasMatch()) { QColor col=jkqtp_lookupQColorName(mNP.captured(1)); double a=QLocale::c().toInt(mNP.captured(2)); col.setAlphaF(a/255.0); return col; } #else QRegExp rxP("(.+)\\s*,\\s*t?\\s*(\\d+\\.?\\d+)\\%"); QRegExp rxAP("(.+)\\s*,\\s*a\\s*(\\d+\\.?\\d+)\\%"); QRegExp rxNP("(.+)\\s*,\\s*a?\\s*([\\d]+)"); if (rxP.exactMatch(color)) { QColor col=jkqtp_lookupQColorName(rxP.cap(1)); double a=QLocale::c().toDouble(rxP.cap(2)); col.setAlphaF(1.0-a/100.0); return col; } if (rxAP.exactMatch(color)) { QColor col=jkqtp_lookupQColorName(rxAP.cap(1)); double a=QLocale::c().toDouble(rxAP.cap(2)); col.setAlphaF(a/100.0); return col; } if (rxNP.exactMatch(color)) { QColor col=jkqtp_lookupQColorName(rxNP.cap(1)); double a=QLocale::c().toInt(rxNP.cap(2)); col.setAlphaF(a/255.0); return col; } #endif return jkqtp_lookupQColorName(color); } std::string jkqtp_to_valid_variable_name(const std::string& input) { std::string out=""; for (size_t i=0; i0)) out=out+input[i]; if ((input[i]=='_')&&(out.size()>0)) out=out+input[i]; } return out; } std::string jkqtp_chartostr(char data){ std::ostringstream ost; ost<& data, const QString& separator) { QString r=""; QLocale loc=QLocale::c(); loc.setNumberOptions(QLocale::OmitGroupSeparator); for (int i=0; i0) r=r+separator; QVariant v=data[i]; #if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0)) if (v.typeId()==QMetaType::Bool) r=r+loc.toString(v.toBool()); else if (v.typeId()==QMetaType::Char) r=r+loc.toString(v.toInt()); else if (v.typeId()==QMetaType::QDate) r=r+loc.toString(v.toDate()); else if (v.typeId()==QMetaType::QDateTime) r=r+loc.toString(v.toDateTime()); else if (v.typeId()==QMetaType::Double) r=r+loc.toString(v.toDouble()); else if (v.typeId()==QMetaType::Int) r=r+loc.toString(v.toInt()); else if (v.typeId()==QMetaType::LongLong) r=r+loc.toString(v.toLongLong()); else if (v.typeId()==QMetaType::QString) r=r+QString("\"%1\"").arg(v.toString().replace("\"", "_").replace("\t", " ").replace("\r", "").replace("\n", " ").replace(",", " ").replace(";", " ")); else if (v.typeId()==QMetaType::QTime) r=r+loc.toString(v.toTime()); else if (v.typeId()==QMetaType::UInt) r=r+loc.toString(v.toUInt()); else if (v.typeId()==QMetaType::ULongLong) r=r+loc.toString(v.toULongLong()); else r=r+v.toString(); #else switch (v.type()) { case QVariant::Bool: r=r+loc.toString(v.toBool()); break; case QVariant::Char: r=r+loc.toString(v.toInt()); break; case QVariant::Date: r=r+loc.toString(v.toDate()); break; case QVariant::DateTime: r=r+loc.toString(v.toDateTime()); break; case QVariant::Double: r=r+loc.toString(v.toDouble()); break; case QVariant::Int: r=r+loc.toString(v.toInt()); break; case QVariant::LongLong: r=r+loc.toString(v.toLongLong()); break; case QVariant::String: r=r+QString("\"%1\"").arg(v.toString().replace("\"", "_").replace("\t", " ").replace("\r", "").replace("\n", " ").replace(",", " ").replace(";", " ")); break; case QVariant::Time: r=r+loc.toString(v.toTime()); break; case QVariant::UInt: r=r+loc.toString(v.toUInt()); break; case QVariant::ULongLong: r=r+loc.toString(v.toULongLong()); break; //case : r=r+loc.toString(v.); break; default: r=r+v.toString(); break; } #endif } return r; } JKQTCOMMON_LIB_EXPORT QString jkqtp_filenameize(const QString& data) { QString r; QString data1=data.simplified(); for (int i=0; i=QT_VERSION_CHECK(6, 0, 0)) const auto c=data1[i]; #else QCharRef c=data1[i]; #endif if (c.isLetterOrNumber() || (c=='-') || (c=='_') || (c=='.')) { r+=c; } else { r+='_'; } } return r; } QString jkqtp_toValidVariableName(const QString& input) { QString out=""; for (int i=0; i0)) out=out+input[i]; if ((input[i]=='_')&&(out.size()>0)) out=out+input[i]; } return out; } QString jkqtp_KeyboardModifiers2String(Qt::KeyboardModifiers modifiers, bool useNONE) { if (modifiers==Qt::NoModifier) { if (useNONE) return "NONE"; else return ""; } QString ret=""; auto append=[](QString& ret, const QString & appending, const QString& separator="+") { if (appending.size()<=0) return; if (ret.size()>0) ret+=separator; ret+=appending; }; if ((modifiers&Qt::ShiftModifier)==Qt::ShiftModifier) append(ret, "SHIFT", "+"); if ((modifiers&Qt::ControlModifier)==Qt::ControlModifier) append(ret, "CTRL", "+"); if ((modifiers&Qt::AltModifier)==Qt::AltModifier) append(ret, "ALT", "+"); if ((modifiers&Qt::MetaModifier)==Qt::MetaModifier) append(ret, "META", "+"); if ((modifiers&Qt::KeypadModifier)==Qt::KeypadModifier) append(ret, "KEYPAD", "+"); if ((modifiers&Qt::GroupSwitchModifier)==Qt::GroupSwitchModifier) append(ret, "GROUP", "+"); return ret; } Qt::KeyboardModifiers jkqtp_String2KeyboardModifiers(const QString &modifiers) { auto mods=modifiers.toUpper().split("+"); Qt::KeyboardModifiers ret=Qt::NoModifier; for (const auto& m: mods) { if (m.trimmed()=="SHIFT") ret |= Qt::ShiftModifier; else if (m.trimmed()=="CTRL") ret |= Qt::ControlModifier; else if (m.trimmed()=="ALT") ret |= Qt::AltModifier; else if (m.trimmed()=="META") ret |= Qt::MetaModifier; else if (m.trimmed()=="KEYPAD") ret |= Qt::KeypadModifier; else if (m.trimmed()=="GROUP") ret |= Qt::GroupSwitchModifier; } return ret; } QString jkqtp_MouseButton2String(Qt::MouseButton button, bool useNONE) { if (button==Qt::NoButton) { if (useNONE) return "NONE"; else return ""; } if (button==Qt::LeftButton) return "LEFT"; if (button==Qt::RightButton) return "RIGHT"; #if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0)) if (button==Qt::MiddleButton) return "MIDDLE"; #else if (button==Qt::MidButton) return "MIDDLE"; #endif if (button==Qt::BackButton) return "BACK"; if (button==Qt::ForwardButton) return "FORWARD"; if (button==Qt::TaskButton) return "TASK"; if (button==Qt::ExtraButton4) return "EXTRA4"; if (button==Qt::ExtraButton5) return "EXTRA5"; if (button==Qt::ExtraButton6) return "EXTRA6"; if (button==Qt::ExtraButton7) return "EXTRA7"; if (button==Qt::ExtraButton8) return "EXTRA8"; if (button==Qt::ExtraButton9) return "EXTRA9"; if (button==Qt::ExtraButton10) return "EXTRA10"; if (button==Qt::ExtraButton11) return "EXTRA11"; if (button==Qt::ExtraButton12) return "EXTRA12"; if (button==Qt::ExtraButton13) return "EXTRA13"; if (button==Qt::ExtraButton14) return "EXTRA14"; if (button==Qt::ExtraButton15) return "EXTRA15"; if (button==Qt::ExtraButton16) return "EXTRA16"; if (button==Qt::ExtraButton17) return "EXTRA17"; if (button==Qt::ExtraButton18) return "EXTRA18"; if (button==Qt::ExtraButton19) return "EXTRA19"; if (button==Qt::ExtraButton20) return "EXTRA20"; if (button==Qt::ExtraButton21) return "EXTRA21"; if (button==Qt::ExtraButton22) return "EXTRA22"; if (button==Qt::ExtraButton23) return "EXTRA23"; if (button==Qt::ExtraButton24) return "EXTRA24"; return "UNKNOWN"; } Qt::MouseButton jkqtp_String2MouseButton(const QString &button) { auto but=button.toUpper().trimmed(); if (but=="LEFT") return Qt::LeftButton; if (but=="RIGHT") return Qt::RightButton; #if (QT_VERSION>=QT_VERSION_CHECK(6, 0, 0)) if (but=="MIDDLE") return Qt::MiddleButton; #else if (but=="MIDDLE") return Qt::MidButton; #endif if (but=="BACK") return Qt::BackButton; if (but=="FORWARD") return Qt::ForwardButton; if (but=="TASK") return Qt::TaskButton; if (but=="EXTRA4") return Qt::ExtraButton4; if (but=="EXTRA5") return Qt::ExtraButton5; if (but=="EXTRA6") return Qt::ExtraButton6; if (but=="EXTRA7") return Qt::ExtraButton7; if (but=="EXTRA8") return Qt::ExtraButton8; if (but=="EXTRA9") return Qt::ExtraButton9; if (but=="EXTRA10") return Qt::ExtraButton10; if (but=="EXTRA11") return Qt::ExtraButton11; if (but=="EXTRA12") return Qt::ExtraButton12; if (but=="EXTRA13") return Qt::ExtraButton13; if (but=="EXTRA14") return Qt::ExtraButton14; if (but=="EXTRA15") return Qt::ExtraButton15; if (but=="EXTRA16") return Qt::ExtraButton16; if (but=="EXTRA17") return Qt::ExtraButton17; if (but=="EXTRA18") return Qt::ExtraButton18; if (but=="EXTRA19") return Qt::ExtraButton19; if (but=="EXTRA20") return Qt::ExtraButton20; if (but=="EXTRA21") return Qt::ExtraButton21; if (but=="EXTRA22") return Qt::ExtraButton22; if (but=="EXTRA23") return Qt::ExtraButton23; if (but=="EXTRA24") return Qt::ExtraButton24; return Qt::NoButton; } std::string jkqtp_UnicodeToUTF8(uint32_t codepoint) { std::string out; if (codepoint <= 0x7f) out.append(1, static_cast(codepoint)); else if (codepoint <= 0x7ff) { out.append(1, static_cast(0xc0 | ((codepoint >> 6) & 0x1f))); out.append(1, static_cast(0x80 | (codepoint & 0x3f))); } else if (codepoint <= 0xffff) { out.append(1, static_cast(0xe0 | ((codepoint >> 12) & 0x0f))); out.append(1, static_cast(0x80 | ((codepoint >> 6) & 0x3f))); out.append(1, static_cast(0x80 | (codepoint & 0x3f))); } else { out.append(1, static_cast(0xf0 | ((codepoint >> 18) & 0x07))); out.append(1, static_cast(0x80 | ((codepoint >> 12) & 0x3f))); out.append(1, static_cast(0x80 | ((codepoint >> 6) & 0x3f))); out.append(1, static_cast(0x80 | (codepoint & 0x3f))); } return out; } QString jkqtp_backslashEscape(const QString &txt) { QString res; for (const QChar c: txt) { if (c=='\n') res+="\\n"; else if (c=='\r') res+="\\r"; else if (c=='\t') res+="\\t"; else if (c=='\\') res+="\\\\"; else if (c.unicode()<32) res+="\\x"+QString::number(c.unicode(), 16).toUpper(); else res+=c; } return res; }