📄 stringutils.hpp
字号:
#pragma ident "$Id: StringUtils.hpp 841 2007-10-18 21:56:01Z renfroba $"//============================================================================//// This file is part of GPSTk, the GPS Toolkit.//// The GPSTk is free software; you can redistribute it and/or modify// it under the terms of the GNU Lesser General Public License as published// by the Free Software Foundation; either version 2.1 of the License, or// any later version.//// The GPSTk 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 for more details.//// You should have received a copy of the GNU Lesser General Public// License along with GPSTk; if not, write to the Free Software Foundation,// Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA// // Copyright 2004, The University of Texas at Austin////============================================================================//============================================================================////This software developed by Applied Research Laboratories at the University of//Texas at Austin, under contract to an agency or agencies within the U.S. //Department of Defense. The U.S. Government retains all rights to use,//duplicate, distribute, disclose, or release this software. ////Pursuant to DoD Directive 523024 //// DISTRIBUTION STATEMENT A: This software has been approved for public // release, distribution is unlimited.////=============================================================================/** * @file StringUtils.hpp * StringUtils namespace and GPSTK string utility functions */#ifndef GPSTK_STRINGUTILS_HPP#define GPSTK_STRINGUTILS_HPP//lgpl-license START//lgpl-license END//dod-release-statement START//dod-release-statement END#include <string>#include <sstream>#include <iomanip>#include <iostream>#include <list>/// @todo Get rid of the stdio.h dependency if possible.#include <cstdio>#include <regex.h>#include <cctype>#include <limits>#include "Exception.hpp"namespace gpstk{ /** * Stuff to make the C++ string class a little easier to use. All the * functionality here is inlined since they are farily small * functions. * * All functions here will throw gpstk::StringUtils::StringException * on an error. Any std::exception is converted to a * gpstk::StringUtils::StringException so * that's the only exception a user of this class needs to catch. * * For any function that modifies a string, make sure there is a * non-const (std::string&) version and a const (const std::string&) * version. The convention for writing the functions is the non-const * version fully implements the function and the const version calls * the non-const version. * * @sa stringutiltest.cpp for some examples. */ namespace StringUtils { /** @defgroup stringutilsgroup Text String Manipulation Tools */ //@{ /// This is thrown instread of a std::exception when a /// gpstk::StringUtils function fails. /// @ingroup exceptiongroup NEW_EXCEPTION_CLASS(StringException, Exception); /// Class for configuring the appearance of hexDumpData() output class HexDumpDataConfig { public: HexDumpDataConfig() : showIndex(true), hexIndex(true), upperHex(false), idxDigits(4), indexWS(1), groupBy(1), groupWS(1), group2By(8), group2WS(2), bytesPerLine(16), showText(true), separator(0), textWS(4) {} HexDumpDataConfig(bool ashowIndex, bool ahexIndex, bool aupperHex, unsigned aidxDigits, unsigned aindexWS, unsigned agroupBy, unsigned agroupWS, unsigned agroup2By, unsigned agroup2WS, unsigned abytesPerLine, bool ashowText, char aseparator, unsigned atextWS) : showIndex(ashowIndex), hexIndex(ahexIndex), upperHex(aupperHex), idxDigits(aidxDigits), indexWS(aindexWS), groupBy(agroupBy), groupWS(agroupWS), group2By(agroup2By), group2WS(agroup2WS), bytesPerLine(abytesPerLine), showText(ashowText), separator(aseparator), textWS(atextWS) {} bool showIndex; ///< display index into string on each line. bool hexIndex; ///< if true, use hex index numbers (else decimal). bool upperHex; ///< if true, use upper-case hex digits. unsigned idxDigits; ///< number of positions to use for index. unsigned indexWS; ///< number of whitespace charaters between index and data. unsigned groupBy; ///< number of bytes of data to show between spaces. unsigned groupWS; ///< number of whitespace charaters between groups of hex data. unsigned group2By; ///< number of groups to show per 2nd layer group (0=none, must be multiple of groupBy). unsigned group2WS; ///< number of whitespace charaters between 2nd layer groups. unsigned bytesPerLine; ///< number of bytes to display on a line of output (must be evenly divisible by both groupBy and group2By). bool showText; ///< if true, show text of message (unprintable characters become '.'. char separator; ///< character to offset text with (0 = none). unsigned textWS; ///< number of whitespace characters between hex and text. }; /** * Perform a formatted hex-dump of the (potentially) binary * data to the given stream. * @param s stream to dump data to. * @param data data to hex-dump. * @param indent indents the string by that many spaces. * @param cfg formatting configuration. */ inline void hexDumpData(std::ostream& s, const std::string& data, unsigned indent = 0, HexDumpDataConfig cfg = HexDumpDataConfig()); /** * Perform a formatted hex-dump of the (potentially) binary * data to the given stream. * @param s stream to dump data to. * @param data data to hex-dump. * @param tag string to put at the beginning of each line of output. * @param cfg formatting configuration. */ inline void hexDumpData(std::ostream& s, const std::string& data, const std::string& tag, HexDumpDataConfig cfg = HexDumpDataConfig()); /** * Remove a string from the beginning of another string. * Occurrences of the string \a aString appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param aString string to remove. * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string& stripLeading(std::string& s, const std::string& aString, std::string::size_type num = std::string::npos) throw(StringException); /** * Remove a string from the beginning of another string const version. * Occurrences of the string \a aString appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param aString string to remove. * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string stripLeading(const std::string& s, const std::string& aString, std::string::size_type num = std::string::npos) throw(StringException) { std::string t(s); stripLeading(t, aString, num); return t; } /** * Remove a string from the beginning of another string. * Occurrences of the string \a pString appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param pString string to remove. * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string& stripLeading(std::string& s, const char* pString, std::string::size_type num = std::string::npos) throw(StringException) { return stripLeading(s, std::string(pString), num); } /** * Remove a string from the beginning of another string const version. * Occurrences of the string \a pString appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param pString string to remove. * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string stripLeading(const std::string& s, const char* pString, std::string::size_type num = std::string::npos) throw(StringException) { std::string t(s); stripLeading(t, std::string(pString), num); return t; } /** * Strip character(s) from the beginning of a string. * Occurrences of the character \a aCharacter appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param aCharacter character to remove. * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string& stripLeading(std::string& s, const char aCharacter, std::string::size_type num = std::string::npos) throw(StringException) { return stripLeading(s, std::string(1,aCharacter), num); } /** * Strip character(s) from the beginning of a string const version. * Occurrences of the character \a aCharacter appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param aCharacter character to remove. * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string stripLeading(const std::string& s, const char aCharacter, std::string::size_type num = std::string::npos) throw(StringException) { std::string t(s); stripLeading(t, std::string(1,aCharacter), num); return t; } /** * Strip blanks from the beginning of a string. * Occurrences of the space character appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string& stripLeading(std::string& s, std::string::size_type num = std::string::npos) throw(StringException) { return stripLeading(s,std::string(1,' '),num); } /** * Strip blanks from the beginning of a string const version. * Occurrences of the space character appearing * at the beginning of the string \a s are removed. * @param s string to be stripped (modified). * @param num maximum number of occurrences to remove. * @throws StringException if there's a std::exception thrown. * @return a reference to \a s. */ inline std::string stripLeading(const std::string& s, std::string::size_type num = std::string::npos) throw(StringException) { std::string t(s); stripLeading(t,std::string(1,' '),num); return t; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -