📄 stringutils.hpp
字号:
inline std::string doub2sci(const double& d, const std::string::size_type length, const std::string::size_type expLen, const bool showSign = true, const bool checkSwitch = true); /** * Convert scientific notation to FORTRAN notation. * As an example, the string "1.5636E5" becomes " .15636D6". * Note that the first character of the string will be '-' if * the number is negative or ' ' if the first character is positive. * @param aStr string with number to convert * @param startPos start position of number in string * @param length length (in characters) of number, including exponent. * @param expLen length (in characters of exponent, not including sign. * @param checkSwitch will keep the method running as orignially programed * when set to true. If false, the method will always resize exponentials, * produce an exponential with an E instead of a D, and always have a leading * zero. For example -> 0.87654E-0004 or -0.1234E00005. * @throws Exception if the string is not a number in * scientific notation */ inline std::string& sci2for(std::string& aStr, const std::string::size_type startPos = 0, const std::string::size_type length = std::string::npos, const std::string::size_type expLen = 3, const bool checkSwitch = true) throw(StringException); /** * Convert double precision floating point to a string * containing the number in FORTRAN notation. * As an example, the number 156360 becomes ".15636D6". * @param d number to convert. * @param length length (in characters) of number, including exponent. * @param expLen length (in characters of exponent, including sign. * @param checkSwitch if true, keeps the exponential sanity check for * exponentials above three characters in length. If false, it removes * that check. * @return a string containing \a d in FORTRAN notation. */ inline std::string doub2for(const double& d, const std::string::size_type length, const std::string::size_type expLen, const bool checkSwitch = true) throw(StringException); /** * Convert FORTRAN representation of a double precision * floating point in a string to a number. * As an example, the number ".15636D6" becomes 156360. * @param aStr string containing FORTRAN representation of number. * @param startPos beginning of number in string. * @param length length (in characters) of number, including exponent. * @return value of the number. */ inline double for2doub(const std::string& aStr, const std::string::size_type startPos = 0, const std::string::size_type length = std::string::npos); /** * Change a string into printable characters. Control * characters (0-26) are changed to ^@, ^A, etc. Other * non-printable characters are changed to hex sequences * enclosed in <>. * @param aStr the string to make printable. */ inline std::string printable(const std::string& aStr) throw(StringException); /** * Nicely expands the input string into several lines, non-const * version. * @param aStr the string to be modified. * @param lineDelim a string to put between every line. * @param indent an indentataion string used on all but the first line * @param firstIndent is the indentation used on the first line. * @param len the maximum length of string to put on a line. * @param wordDelim the character that separates each word. * @return the string nicely formatted. */ inline std::string& prettyPrint(std::string& aStr, const std::string& lineDelim = "\n", const std::string& indent = "", const std::string& firstIndent = " ", const std::string::size_type len = 80, const char wordDelim = ' ') throw(StringException); /** * Const version of prettyPrint, which nicely expands the * input string into several lines. * @param aStr the string to be modified. * @param lineDelim a string to put between every line. * @param indent an indentataion string used on all but the first line * @param firstIndent is the indentation used on the first line. * @param len the maximum length of string to put on a line. * @param wordDelim the character that separates each word. * @return the string nicely formatted. */ inline std::string prettyPrint(const std::string& aStr, const std::string& lineDelim = "\n", const std::string& indent = "", const std::string& firstIndent = " ", const std::string::size_type len = 80, const char wordDelim = ' ') throw(StringException) { std::string temp(aStr); prettyPrint(temp, lineDelim, indent, firstIndent, len, wordDelim); return temp; } } // namespace StringUtils } // namespace gpstk// ################################################// Implementations of inline functions follow// ################################################namespace gpstk{ namespace StringUtils { inline void hexDumpData(std::ostream& s, const std::string& data, unsigned indent, HexDumpDataConfig cfg) { std::string instr(indent, ' '); hexDumpData(s, data, instr, cfg); } inline void hexDumpData(std::ostream& s, const std::string& data, const std::string& tag, HexDumpDataConfig cfg) { std::string ascii=""; unsigned indent = tag.length(); int col = 0; int datasize=data.size(); std::string groupws(cfg.groupWS, ' '); std::string group2ws(cfg.group2WS, ' '); std::string indexws(cfg.indexWS, ' '); std::string textws(cfg.textWS, ' '); unsigned linesize; if (cfg.groupBy && ((cfg.bytesPerLine % cfg.groupBy) != 0)) { s << "hexDumpData: cfg.bytesPerLine % cfg.groupBy != 0" << std::endl; return; } if (cfg.group2By && ((cfg.bytesPerLine % cfg.group2By) != 0)) { s << "hexDumpData: cfg.bytesPerLine % cfg.group2By != 0" << std::endl; return; } if (cfg.groupBy && ((cfg.group2By % cfg.groupBy) != 0)) { s << "hexDumpData: cfg.group2By % cfg.groupBy != 0" << std::endl; return; } // line format: // <tag><index>:<indexws><group1byte1>...<group1byte[groupBy]><groupws>...<group[group2By]byte1>...<group[group2By]byte[groupBy]><group2ws>....<byte[bytesPerLine]><textws><separator><text><separator>\n linesize = indent; if (cfg.showIndex) linesize += cfg.idxDigits + 1 + cfg.indexWS; linesize += cfg.bytesPerLine * 2; unsigned w2 = 0; unsigned w1 = 0; if (cfg.group2By) w2 = (cfg.bytesPerLine / cfg.group2By) - 1; if (cfg.groupBy) w1 = (cfg.bytesPerLine / cfg.groupBy) - w2 - 1; if (cfg.groupBy > 0) linesize += cfg.groupWS * w1; if (cfg.group2By > 0) linesize += cfg.group2WS * w2; /* linesize doesn't include text stuff if (cfg.showText) linesize += cfg.textWS + cfg.bytesPerLine; if (cfg.separator) linesize += 2; */ for (int i=0; i<datasize; i++) { if (i%cfg.bytesPerLine==0) { s << tag; col = indent; if (cfg.showIndex) { if (cfg.hexIndex) { s << std::hex; if (cfg.upperHex) s << std::uppercase; else s << std::nouppercase; } else s << std::dec; s << std::setfill('0'); s << std::setw(cfg.idxDigits) << i << ":" << indexws; s << std::dec << std::nouppercase; } col += cfg.idxDigits + 1 + cfg.indexWS; } unsigned char c=data[i]; if (isprint(c)) ascii += c; else ascii += '.'; if (cfg.upperHex) s << std::uppercase; else s << std::nouppercase; s << std::hex << std::setw(2) << (int)c << std::dec << std::nouppercase; col += 2; if (((i % cfg.bytesPerLine) == (cfg.bytesPerLine-1)) || (i == (datasize-1))) { if (cfg.showText) { int extra = linesize-col; std::string space(extra, ' '); s << space << textws; if (cfg.separator) s << cfg.separator; s << ascii; if (cfg.separator) s << cfg.separator; s << std::endl; } // this *should* be updated at the beginning of the loop //col=indent+6; ascii.erase(); } else if (cfg.group2By && ((i % cfg.group2By) == (cfg.group2By-1))) { s << group2ws; col += cfg.group2WS; } else if (cfg.groupBy && ((i % cfg.groupBy) == (cfg.groupBy-1))) { s << groupws; col += cfg.groupWS; } } } // Keep searching for aString at the start of s // until num == 0 or aString is not found at the start of s inline std::string& stripLeading(std::string& s, const std::string& aString, std::string::size_type num) throw(StringException) { try { if (aString == "") return s; while((num > 0) && (s.find(aString,0) == 0) && (s.length() > 0)) { s.erase(0,aString.length()); --num; } return s; } catch(std::exception &e) { StringException strexc("Exception thrown: " + std::string(e.what())); GPSTK_THROW(strexc); } } // keep searching for aString at the end of s // until aString isn't found there or num == 0 inline std::string& stripTrailing(std::string& s, const std::string&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -