/* Copyright (c) 2008-2019 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include 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)::epsilon()) { 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)/log(10.0))); 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)::epsilon()) return "0"; double adata=fabs(data); std::string res=jkqtp_floattostr(data, past_comma, remove_trail0); long exp=static_cast(floor(log(adata)/log(10.0))); 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_String2QColor(const QString &color) { QRegExp rxP("(.+)\\s*,\\s*(\\d+\\.?\\d+)\\%"); QRegExp rxNP("(.+)\\s*,\\s*([\\d]+)"); if (rxP.exactMatch(color)) { QColor col(rxP.cap(1)); double a=QLocale::c().toDouble(rxP.cap(2)); col.setAlphaF(a/100.0); return col; } if (rxNP.exactMatch(color)) { QColor col(rxNP.cap(1)); double a=QLocale::c().toInt(rxNP.cap(2)); col.setAlphaF(a/255.0); return col; } return QColor(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<