string.h

来自「A*算法 A*算法 A*算法 A*算法A*算法A*算法」· C头文件 代码 · 共 1,531 行 · 第 1/4 页

H
1,531
字号
  value_type at(size_type n) const
    { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
    // returns the writable character at position n
  reference at(size_type n)
    { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }

  // lib.string.modifiers
    // append elements str[pos], ..., str[pos+n]
  wxStringBase& append(const wxStringBase& str, size_t pos, size_t n)
  {
    wxASSERT(pos <= str.length());
    ConcatSelf(n, str.c_str() + pos, str.length() - pos);
    return *this;
  }
    // append a string
  wxStringBase& append(const wxStringBase& str)
    { ConcatSelf(str.length(), str.c_str()); return *this; }
    // append first n (or all if n == npos) characters of sz
  wxStringBase& append(const wxChar *sz)
    { ConcatSelf(wxStrlen(sz), sz); return *this; }
  wxStringBase& append(const wxChar *sz, size_t n)
    { ConcatSelf(n, sz); return *this; }
    // append n copies of ch
  wxStringBase& append(size_t n, wxChar ch);
    // append from first to last
  wxStringBase& append(const_iterator first, const_iterator last)
    { ConcatSelf(last - first, first); return *this; }

    // same as `this_string = str'
  wxStringBase& assign(const wxStringBase& str)
    { return *this = str; }
    // same as ` = str[pos..pos + n]
  wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n)
    { clear(); return append(str, pos, n); }
    // same as `= first n (or all if n == npos) characters of sz'
  wxStringBase& assign(const wxChar *sz)
    { clear(); return append(sz, wxStrlen(sz)); }
  wxStringBase& assign(const wxChar *sz, size_t n)
    { clear(); return append(sz, n); }
    // same as `= n copies of ch'
  wxStringBase& assign(size_t n, wxChar ch)
    { clear(); return append(n, ch); }
    // assign from first to last
  wxStringBase& assign(const_iterator first, const_iterator last)
    { clear(); return append(first, last); }

    // first valid index position
  const_iterator begin() const { return m_pchData; }
    // position one after the last valid one
  const_iterator end() const { return m_pchData + length(); }

  // first valid index position
  iterator begin();
  // position one after the last valid one
  iterator end();

    // insert another string
  wxStringBase& insert(size_t nPos, const wxStringBase& str)
  {
    wxASSERT( str.GetStringData()->IsValid() );
    return insert(nPos, str.c_str(), str.length());
  }
    // insert n chars of str starting at nStart (in str)
  wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n)
  {
    wxASSERT( str.GetStringData()->IsValid() );
    wxASSERT( nStart < str.length() );
    size_t strLen = str.length() - nStart;
    n = strLen < n ? strLen : n;
    return insert(nPos, str.c_str() + nStart, n);
  }
    // insert first n (or all if n == npos) characters of sz
  wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos);
    // insert n copies of ch
  wxStringBase& insert(size_t nPos, size_t n, wxChar ch)
    { return insert(nPos, wxStringBase(n, ch)); }
  iterator insert(iterator it, wxChar ch)
    { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
  void insert(iterator it, const_iterator first, const_iterator last)
    { insert(it - begin(), first, last - first); }
  void insert(iterator it, size_type n, wxChar ch)
    { insert(it - begin(), n, ch); }

    // delete characters from nStart to nStart + nLen
  wxStringBase& erase(size_type pos = 0, size_type n = npos);
  iterator erase(iterator first, iterator last)
  {
    size_t idx = first - begin();
    erase(idx, last - first);
    return begin() + idx;
  }
  iterator erase(iterator first);

  // explicit conversion to C string (use this with printf()!)
  const wxChar* c_str() const { return m_pchData; }
  const wxChar* data() const { return m_pchData; }

    // replaces the substring of length nLen starting at nStart
  wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz);
    // replaces the substring of length nLen starting at nStart
  wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str)
    { return replace(nStart, nLen, str.c_str()); }
    // replaces the substring with nCount copies of ch
  wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
    // replaces a substring with another substring
  wxStringBase& replace(size_t nStart, size_t nLen,
                        const wxStringBase& str, size_t nStart2, size_t nLen2);
    // replaces the substring with first nCount chars of sz
  wxStringBase& replace(size_t nStart, size_t nLen,
                        const wxChar* sz, size_t nCount);
  wxStringBase& replace(iterator first, iterator last, const_pointer s)
    { return replace(first - begin(), last - first, s); }
  wxStringBase& replace(iterator first, iterator last, const_pointer s,
                        size_type n)
    { return replace(first - begin(), last - first, s, n); }
  wxStringBase& replace(iterator first, iterator last, const wxStringBase& s)
    { return replace(first - begin(), last - first, s); }
  wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c)
    { return replace(first - begin(), last - first, n, c); }
  wxStringBase& replace(iterator first, iterator last,
                        const_iterator first1, const_iterator last1)
    { return replace(first - begin(), last - first, first1, last1 - first1); }

    // swap two strings
  void swap(wxStringBase& str);

    // All find() functions take the nStart argument which specifies the
    // position to start the search on, the default value is 0. All functions
    // return npos if there were no match.

    // find a substring
  size_t find(const wxStringBase& str, size_t nStart = 0) const;

    // find first n characters of sz
  size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;

    // find the first occurence of character ch after nStart
  size_t find(wxChar ch, size_t nStart = 0) const;

    // rfind() family is exactly like find() but works right to left

    // as find, but from the end
  size_t rfind(const wxStringBase& str, size_t nStart = npos) const;

    // as find, but from the end
  size_t rfind(const wxChar* sz, size_t nStart = npos,
               size_t n = npos) const;
    // as find, but from the end
  size_t rfind(wxChar ch, size_t nStart = npos) const;

    // find first/last occurence of any character in the set

    // as strpbrk() but starts at nStart, returns npos if not found
  size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const
    { return find_first_of(str.c_str(), nStart); }
    // same as above
  size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
  size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const;
    // same as find(char, size_t)
  size_t find_first_of(wxChar c, size_t nStart = 0) const
    { return find(c, nStart); }
    // find the last (starting from nStart) char from str in this string
  size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const
    { return find_last_of(str.c_str(), nStart); }
    // same as above
  size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
  size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const;
    // same as above
  size_t find_last_of(wxChar c, size_t nStart = npos) const
    { return rfind(c, nStart); }

    // find first/last occurence of any character not in the set

    // as strspn() (starting from nStart), returns npos on failure
  size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const
    { return find_first_not_of(str.c_str(), nStart); }
    // same as above
  size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
  size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const;
    // same as above
  size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
    //  as strcspn()
  size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const
    { return find_last_not_of(str.c_str(), nStart); }
    // same as above
  size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
  size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const;
    // same as above
  size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;

    // All compare functions return -1, 0 or 1 if the [sub]string is less,
    // equal or greater than the compare() argument.

    // comparison with another string
  int compare(const wxStringBase& str) const;
    // comparison with a substring
  int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
    // comparison of 2 substrings
  int compare(size_t nStart, size_t nLen,
              const wxStringBase& str, size_t nStart2, size_t nLen2) const;
    // comparison with a c string
  int compare(const wxChar* sz) const;
    // substring comparison with first nCount characters of sz
  int compare(size_t nStart, size_t nLen,
              const wxChar* sz, size_t nCount = npos) const;

  size_type copy(wxChar* s, size_type n, size_type pos = 0);

  // substring extraction
  wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const;

      // string += string
  wxStringBase& operator+=(const wxStringBase& s) { return append(s); }
      // string += C string
  wxStringBase& operator+=(const wxChar *psz) { return append(psz); }
      // string += char
  wxStringBase& operator+=(wxChar ch) { return append(1, ch); }
};

#endif // !wxUSE_STL

// ----------------------------------------------------------------------------
// wxString: string class trying to be compatible with std::string, MFC
//           CString and wxWindows 1.x wxString all at once
// ---------------------------------------------------------------------------

class WXDLLIMPEXP_BASE wxString : public wxStringBase
{
#if !wxUSE_STL
friend class WXDLLIMPEXP_BASE wxArrayString;
#endif

  // NB: special care was taken in arranging the member functions in such order
  //     that all inline functions can be effectively inlined, verify that all
  //     performace critical functions are still inlined if you change order!
private:
  // if we hadn't made these operators private, it would be possible to
  // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
  // converted to char in C and we do have operator=(char)
  //
  // NB: we don't need other versions (short/long and unsigned) as attempt
  //     to assign another numeric type to wxString will now result in
  //     ambiguity between operator=(char) and operator=(int)
  wxString& operator=(int);

  // these methods are not implemented - there is _no_ conversion from int to
  // string, you're doing something wrong if the compiler wants to call it!
  //
  // try `s << i' or `s.Printf("%d", i)' instead
  wxString(int);

public:
  // constructors and destructor
    // ctor for an empty string
  wxString() : wxStringBase() { }
    // copy ctor
  wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { }
  wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { }
    // string containing nRepeat copies of ch
  wxString(wxChar ch, size_t nRepeat = 1)
      : wxStringBase(nRepeat, ch) { }
  wxString(size_t nRepeat, wxChar ch)
      : wxStringBase(nRepeat, ch) { }
    // ctor takes first nLength characters from C string
    // (default value of npos means take all the string)
  wxString(const wxChar *psz)
      : wxStringBase(psz ? psz : wxT("")) { }
  wxString(const wxChar *psz, size_t nLength)
      : wxStringBase(psz, nLength) { }
  wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = npos)
      : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { }

  // even we're not build with wxUSE_STL == 1 it is very convenient to allow
  // implicit conversions from std::string to wxString as this allows to use
  // the same strings in non-GUI and GUI code, however we don't want to
  // unconditionally add this ctor as it would make wx lib dependent on
  // libstdc++ on some Linux versions which is bad, so instead we ask the
  // client code to define this wxUSE_STD_STRING symbol if they need it
#if wxUSE_STD_STRING
  wxString(const wxStdString& s)
      : wxStringBase(s.c_str()) { }
#endif // wxUSE_STD_STRING

#if wxUSE_UNICODE
    // from multibyte string
  wxString(const char *psz, wxMBConv& conv, size_t nLength = npos);
    // from wxWCharBuffer (i.e. return from wxGetString)
  wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { }
#else // ANSI
    // from C string (for compilers using unsigned char)
  wxString(const unsigned char* psz, size_t nLength = npos)
      : wxStringBase((const char*)psz, nLength) { }

#if wxUSE_WCHAR_T
    // from wide (Unicode) string
  wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = npos);
#endif // !wxUSE_WCHAR_T

    // from wxCharBuffer
  wxString(const wxCharBuffer& psz)
      : wxStringBase(psz) { }
#endif // Unicode/ANSI

  // generic attributes & operations
    // as standard strlen()
  size_t Len() const { return length(); }
    // string contains any characters?
  bool IsEmpty() const { return empty(); }
    // empty string is "false", so !str will return true
  bool operator!() const { return IsEmpty(); }
    // truncate the string to given length
  wxString& Truncate(size_t uiLen);
    // empty string contents
  void Empty()
  {
    Truncate(0);

    wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
  }
    // empty the string and free memory
  void Clear()
  {
    wxString tmp(wxEmptyString);
    swap(tmp);
  }

  // contents test
    // Is an ascii value
  bool IsAscii() const;
    // Is a number
  bool IsNumber() const;
    // Is a word
  bool IsWord() const;

  // data access (all indexes are 0 based)
    // read access
    wxChar  GetChar(size_t n) const
      { return at(n); }
    // read/write access
    wxChar& GetWritableChar(size_t n)
      { return at(n); }
    // write access
    void  SetChar(size_t n, wxChar ch)
      { at(n) = ch; }

    // get last character
    wxChar  Last() const
      {
          wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );

          return at(length() - 1);
      }

    // get writable last character
    wxChar& Last()
      {
          wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
          return at(length() - 1);
      }

    /*
       Note that we we must define all of the overloads below to avoid
       ambiguity when using str[0]. Also note that for a conforming compiler we
       don't need const version of operatorp[] at all as indexed access to
       const string is provided by implicit conversion to "const wxChar *"
       below and defining them would only result in ambiguities, but some other
       compilers refuse to compile "str[0]" without them.
     */

#if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
    wxChar operator[](int n) const
      { return wxStringBase::at(n); }
    wxChar operator[](size_type n) const
      { return wxStringBase::at(n); }
#ifndef wxSIZE_T_IS_UINT
    wxChar operator[](unsigned int n) const
      { return wxStringBase::at(n); }
#endif // size_t != unsigned int
#endif // broken compiler


    // operator versions of GetWriteableChar()
    wxChar& operator[](int n)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?