Changeset 33805 in vbox for trunk/include/iprt/cpp
- Timestamp:
- Nov 5, 2010 5:17:16 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/include/iprt/cpp/ministring.h
r33694 r33805 76 76 * This allocates s.length() + 1 bytes for the new instance, unless s is empty. 77 77 * 78 * @param sThe source string.78 * @param a_rSrc The source string. 79 79 * 80 80 * @throws std::bad_alloc 81 81 */ 82 MiniString(const MiniString & s)83 { 84 copyFrom (s);82 MiniString(const MiniString &a_rSrc) 83 { 84 copyFromN(a_rSrc.m_psz, a_rSrc.m_cch); 85 85 } 86 86 … … 96 96 MiniString(const char *pcsz) 97 97 { 98 copyFrom(pcsz); 98 copyFromN(pcsz, strlen(pcsz)); 99 } 100 101 /** 102 * Create a partial copy of another MiniString. 103 * 104 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes) 105 * to copy from the source string. 106 * @param a_rSrc The source string. 107 */ 108 MiniString(size_t a_cchSrc, const MiniString &a_rSrc) 109 { 110 Assert(a_cchSrc <= a_rSrc.m_cch); 111 copyFromN(a_rSrc.m_psz, RT_MIN(a_cchSrc, a_rSrc.m_cch)); 112 } 113 114 /** 115 * Create a partial copy of a C string. 116 * 117 * @param a_cchSrc The max number of chars (encoded UTF-8 bytes) 118 * to copy from the source string. 119 * @param a_pszSrc The source string (UTF-8). 120 */ 121 MiniString(size_t a_cchSrc, const char *a_pszSrc) 122 { 123 size_t cchMax = a_pszSrc ? RTStrNLen(a_pszSrc, a_cchSrc) : 0; 124 Assert(a_cchSrc <= cchMax); 125 copyFromN(a_pszSrc, RT_MIN(a_cchSrc, cchMax)); 99 126 } 100 127 … … 204 231 { 205 232 cleanup(); 206 copyFrom (pcsz);233 copyFromN(pcsz, pcsz ? strlen(pcsz) : 0); 207 234 } 208 235 return *this; … … 224 251 { 225 252 cleanup(); 226 copyFrom (s);253 copyFromN(s.m_psz, s.m_cch); 227 254 } 228 255 return *this; … … 731 758 732 759 /** 733 * Protected internal helper to copy a string. This ignores the previous object 734 * state, so either call this from a constructor or call cleanup() first. 735 * 736 * copyFrom() unconditionally sets the members to a copy of the given other 737 * strings and makes no assumptions about previous contents. Can therefore be 738 * used both in copy constructors, when member variables have no defined value, 739 * and in assignments after having called cleanup(). 740 * 741 * This variant copies from another MiniString and is fast since 742 * the length of the source string is known. 743 * 744 * @param s The source string. 745 * 746 * @throws std::bad_alloc On allocation failure. The object is left describing 747 * a NULL string. 748 */ 749 void copyFrom(const MiniString &s) 750 { 751 if ((m_cch = s.m_cch)) 752 { 753 m_cbAllocated = m_cch + 1; 754 m_psz = (char *)RTStrAlloc(m_cbAllocated); 760 * Protected internal helper to copy a string. 761 * 762 * This ignores the previous object state, so either call this from a 763 * constructor or call cleanup() first. copyFromN() unconditionally sets 764 * the members to a copy of the given other strings and makes no 765 * assumptions about previous contents. Can therefore be used both in copy 766 * constructors, when member variables have no defined value, and in 767 * assignments after having called cleanup(). 768 * 769 * @param pcszSrc The source string. 770 * @param cchSrc The number of chars (bytes) to copy from the 771 * source strings. 772 * 773 * @throws std::bad_alloc On allocation failure. The object is left 774 * describing a NULL string. 775 */ 776 void copyFromN(const char *pcszSrc, size_t cchSrc) 777 { 778 if (cchSrc) 779 { 780 m_psz = RTStrAlloc(cchSrc + 1); 755 781 if (RT_LIKELY(m_psz)) 756 memcpy(m_psz, s.m_psz, m_cbAllocated); // include 0 terminator 782 { 783 m_cch = cchSrc; 784 m_cbAllocated = cchSrc + 1; 785 memcpy(m_psz, pcszSrc, cchSrc + 1); 786 } 757 787 else 758 788 { … … 766 796 else 767 797 { 768 m_cbAllocated = 0;769 m_psz = NULL;770 }771 }772 773 /**774 * Protected internal helper to copy a string. This ignores the previous object775 * state, so either call this from a constructor or call cleanup() first.776 *777 * See copyFrom() above.778 *779 * This variant copies from a C string and needs to call strlen()780 * on it. It's therefore slower than the one above.781 *782 * @param pcsz The source string.783 *784 * @throws std::bad_alloc On allocation failure. The object is left describing785 * a NULL string.786 */787 void copyFrom(const char *pcsz)788 {789 if (pcsz && *pcsz)790 {791 m_cch = strlen(pcsz);792 m_cbAllocated = m_cch + 1;793 m_psz = (char *)RTStrAlloc(m_cbAllocated);794 if (RT_LIKELY(m_psz))795 memcpy(m_psz, pcsz, m_cbAllocated); // include 0 terminator796 else797 {798 m_cch = 0;799 m_cbAllocated = 0;800 #ifdef RT_EXCEPTIONS_ENABLED801 throw std::bad_alloc();802 #endif803 }804 }805 else806 {807 798 m_cch = 0; 808 799 m_cbAllocated = 0;
Note:
See TracChangeset
for help on using the changeset viewer.