📄 mstring.cxx
字号:
/********************************************************************* $Id: mstring.cxx,v 1.2 1999/08/31 02:22:05 cullen Exp $ ********************************************************************* This library 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 of the License, or (at your option) any later version. In addition to the terms and conditions set forth in the GNU Lesser General Public License, as a condition of using this library you are required to grant to all users of this library or any implementation utilizing or derived from this library a reciprocal, no cost, worldwide, perpetual, non-exclusive, non-transferable, unrestricted license to your claims of all patents and patent applications throughout the world that are infringed by the library or any implementation utilizing or derived from this library. In the event you redistribute this library or any implementation utilizing or derived from this library, you must prominently display the foregoing terms and conditions with the library or the implementation utilizing or derived from this library. In the event of a conflict of terms between the foregoing license grant and the terms set forth in the GNU Lesser General Public License, the foregoing terms and conditions shall be deemed to govern. This library 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 this library; if not; write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. Copyright 1999 Vovida Networks, Inc. All Rights Reserved. ********************************************************************* $Log: mstring.cxx,v $ Revision 1.2 1999/08/31 02:22:05 cullen updated header * SIP Parser mstring class - auxilliary string class implementation * Implemented by Maxim A.Shemanaryov **********************************************************************/#include <stdio.h>#include "mstring.h"namespace std{ //-------------------------------------------------------------------------- mstring & mstring::assigni(int val, const char *format) { char buf[64]; sprintf(buf, format, val); return *this = buf; } //-------------------------------------------------------------------------- mstring & mstring::assignd(double val, const char *format) { char buf[64]; sprintf(buf, format, val); return *this = buf; } //-------------------------------------------------------------------------- mstring & mstring::ltrims(const string &s) { erase(0, find_first_not_of(s)); return *this; } //-------------------------------------------------------------------------- mstring & mstring::rtrims(const string &s) { size_type pos = find_last_not_of(s); if(pos == string::npos) pos = 0; else pos++; erase(pos); return *this; } //-------------------------------------------------------------------------- mstring & mstring::toleft(unsigned newlen, char fill, bool cut) { unsigned lstr = length(); if(lstr < newlen) { insert(lstr, newlen - lstr, fill); } if(cut && newlen < lstr) { erase(newlen, string::npos); } return *this; } //-------------------------------------------------------------------------- mstring & mstring::toright(unsigned newlen, char fill, bool cut) { unsigned lstr = length(); if(lstr < newlen) { insert(size_type(0), newlen - lstr, fill); } if(cut && newlen < lstr) { erase(size_type(0), lstr - newlen); } return *this; } //-------------------------------------------------------------------------- unsigned mstring::replace_all(const string &find_str, const string &repl_str) { size_type pos = 0; size_type find_len = find_str.length(); size_type repl_len = repl_str.length(); unsigned nrepl = 0; while((pos = find(find_str, pos)) != string::npos) { replace(pos, find_len, repl_str); pos += repl_len; nrepl++; } return nrepl; } //-------------------------------------------------------------------------- int mstring::compare(const string &str, size_type len) const { if(len == string::npos) { return strcmp(c_str(), str.c_str()); } return strncmp(c_str(), str.c_str(), len); } //-------------------------------------------------------------------------- int mstring::comparei(const string &str, size_type len) const {#ifdef WIN32#define strcasecmp stricmp#define strncasecmp strnicmp#endif if(len == string::npos) { return strcasecmp(c_str(), str.c_str()); } return strncasecmp(c_str(), str.c_str(), len);#ifdef WIN32#undef strcasecmp#undef strncasecmp#endif } //-------------------------------------------------------------------------- mstring & mstring::tab2sp(unsigned tab_size) { size_type pos = 0; while((pos = find('\t')) != string::npos) { size_type n_sp = tab_size - pos % tab_size; replace(pos, 1, n_sp, ' '); pos += n_sp; } return *this; } //--------------------------------------------------------------------------------- mstring & mstring::ins_pair(const string &chrset, char pair_symb) { string::size_type i; char prev = 0; for(i = 0; i < length(); ++i) { if((chrset.find((*this)[i]) != string::npos) == (prev != pair_symb)) { insert(i++, 1, pair_symb); } prev = (*this)[i]; } if(i > 0 && (*this)[i-1] == pair_symb) append(1, pair_symb); return *this; } //--------------------------------------------------------------------------------- mstring & mstring::del_pair(char pair_symb) { string::size_type i; for(i = 0; i < length(); i++) { if((*this)[i] == pair_symb) { erase(i, 1); if((*this)[i] == 0) break; } } return *this; } //--------------------------------------------------------------------------------- mstring & mstring::ins_escaped(const string &chrset, char escaped_symb) { ///!!! for further implementation return *this; } //--------------------------------------------------------------------------------- mstring & mstring::del_escaped(char escaped_symb) { ///!!! for further implementation return *this; } //--------------------------------------------------------------------------------- mstring & mstring::quote(const string &q_start, const string &q_end) { insert(0, q_start); append(q_end); return *this; } //--------------------------------------------------------------------------------- /** Get the next token from string @return: the next position in string to be continue or string::npos if no more tokens @param start Start position of the string - 0 or the previous returned value @param sep String with possible separators. Every symbol of this string is separator @param quote String with quotation symbols, for example, "'" Inside quote string is not divided. Quotation can start with any of these symbols, but finish with the same symbol only. @param pair_chr Usually '\' symbol to include quotation symbols inside the quote parts @param sep_flag Flag sep_single or sep_multiple to determine, whether the separators, going successively are nerpreted as detached empty tokens (sep_single) or one token (sep_multiple), as strtok does. sep_single: string "123,,456" will be "123", "", "456" sep_multiple: string "123,,456" will be "123", "456" \\================================================================================*/ string::size_type mstring::next_token(size_type start, const string &sep, const string "e, char pair_chr, sep_flag flag) const { size_type count = 0; char quote_chr = 0; if(start == string::npos) return string::npos; const char *pstr = c_str() + start; if(*pstr == 0) return string::npos; if(flag == sep_multiple) { //Pass all the separators symbols at the begin of the string while(*pstr && sep.find(*pstr) != string::npos) { ++pstr; ++start; } } for(count = 0;;count++) { char c = *pstr++; if(c == 0) { start = string::npos; break; } //Outside quote find one of separator symbols if(quote_chr == 0 && sep.find(c) != string::npos) { start += count; break; } //Switch quote. If it is not a quote yet, try to check any of //quote symbols. Otherwise quote must be finished with quote_symb if(quote_chr == 0) { if(quote.find(c) != string::npos) { quote_chr = c; continue; } } else { //Inside quote pass all the pair symbols if(pair_chr && c == pair_chr) { if(*pstr) { ++count; ++pstr; } continue; } if(c == quote_chr) { quote_chr = 0; continue; } } } return start; } //--------------------------------------------------------------------------------- /**
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -