StringBuilder.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  1. // -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
  2. //
  3. // System.Text.StringBuilder
  4. //
  5. // Authors:
  6. // Marcin Szczepanski ([email protected])
  7. // Paolo Molaro ([email protected])
  8. // Patrik Torstensson
  9. //
  10. // NOTE: In the case the buffer is only filled by 50% a new string
  11. // will be returned by ToString() is cached in the '_cached_str'
  12. // cache_string will also control if a string has been handed out
  13. // to via ToString(). If you are chaning the code make sure that
  14. // if you modify the string data set the cache_string to null.
  15. //
  16. //
  17. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  18. // Copyright 2011 Xamarin Inc
  19. //
  20. // Permission is hereby granted, free of charge, to any person obtaining
  21. // a copy of this software and associated documentation files (the
  22. // "Software"), to deal in the Software without restriction, including
  23. // without limitation the rights to use, copy, modify, merge, publish,
  24. // distribute, sublicense, and/or sell copies of the Software, and to
  25. // permit persons to whom the Software is furnished to do so, subject to
  26. // the following conditions:
  27. //
  28. // The above copyright notice and this permission notice shall be
  29. // included in all copies or substantial portions of the Software.
  30. //
  31. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  32. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  33. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  34. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  35. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  36. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  37. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  38. //
  39. using System.Runtime.Serialization;
  40. using System.Runtime.CompilerServices;
  41. using System.Runtime.InteropServices;
  42. namespace System.Text {
  43. [Serializable]
  44. [ComVisible (true)]
  45. [MonoLimitation ("Serialization format not compatible with .NET")]
  46. [StructLayout (LayoutKind.Sequential)]
  47. public sealed class StringBuilder : ISerializable
  48. {
  49. private int _length;
  50. private string _str;
  51. private string _cached_str;
  52. private int _maxCapacity;
  53. private const int constDefaultCapacity = 16;
  54. public StringBuilder(string value, int startIndex, int length, int capacity)
  55. : this (value, startIndex, length, capacity, Int32.MaxValue)
  56. {
  57. }
  58. private StringBuilder(string value, int startIndex, int length, int capacity, int maxCapacity)
  59. {
  60. // first, check the parameters and throw appropriate exceptions if needed
  61. if (null == value)
  62. value = "";
  63. // make sure startIndex is zero or positive
  64. if (startIndex < 0)
  65. throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex cannot be less than zero.");
  66. // make sure length is zero or positive
  67. if(length < 0)
  68. throw new System.ArgumentOutOfRangeException ("length", length, "Length cannot be less than zero.");
  69. if (capacity < 0)
  70. throw new System.ArgumentOutOfRangeException ("capacity", capacity, "capacity must be greater than zero.");
  71. if (maxCapacity < 1)
  72. throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
  73. if (capacity > maxCapacity)
  74. throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
  75. // make sure startIndex and length give a valid substring of value
  76. // re-ordered to avoid possible integer overflow
  77. if (startIndex > value.Length - length)
  78. throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex and length must refer to a location within the string.");
  79. if (capacity == 0) {
  80. if (maxCapacity > constDefaultCapacity)
  81. capacity = constDefaultCapacity;
  82. else
  83. _str = _cached_str = String.Empty;
  84. }
  85. _maxCapacity = maxCapacity;
  86. if (_str == null)
  87. _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
  88. if (length > 0)
  89. String.CharCopy (_str, 0, value, startIndex, length);
  90. _length = length;
  91. }
  92. public StringBuilder () : this (null) {}
  93. public StringBuilder(int capacity) : this (String.Empty, 0, 0, capacity) {}
  94. public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity, maxCapacity) { }
  95. public StringBuilder (string value)
  96. {
  97. /*
  98. * This is an optimization to avoid allocating the internal string
  99. * until the first Append () call.
  100. * The runtime pinvoke marshalling code needs to be aware of this.
  101. */
  102. if (null == value)
  103. value = "";
  104. _length = value.Length;
  105. _str = _cached_str = value;
  106. _maxCapacity = Int32.MaxValue;
  107. }
  108. public StringBuilder( string value, int capacity) : this(value == null ? "" : value, 0, value == null ? 0 : value.Length, capacity) {}
  109. public int MaxCapacity {
  110. get {
  111. return _maxCapacity;
  112. }
  113. }
  114. public int Capacity {
  115. get {
  116. if (_str.Length == 0)
  117. return Math.Min (_maxCapacity, constDefaultCapacity);
  118. return _str.Length;
  119. }
  120. set {
  121. if (value < _length)
  122. throw new ArgumentException( "Capacity must be larger than length" );
  123. if (value > _maxCapacity)
  124. throw new ArgumentOutOfRangeException ("value", "Should be less than or equal to MaxCapacity");
  125. InternalEnsureCapacity(value);
  126. }
  127. }
  128. public int Length {
  129. get {
  130. return _length;
  131. }
  132. set {
  133. if( value < 0 || value > _maxCapacity)
  134. throw new ArgumentOutOfRangeException();
  135. if (value == _length)
  136. return;
  137. if (value < _length) {
  138. // LAMESPEC: The spec is unclear as to what to do
  139. // with the capacity when truncating the string.
  140. // Do as MS, keep the capacity
  141. // Make sure that we invalidate any cached string.
  142. InternalEnsureCapacity (value);
  143. _length = value;
  144. } else {
  145. // Expand the capacity to the new length and
  146. // pad the string with NULL characters.
  147. Append('\0', value - _length);
  148. }
  149. }
  150. }
  151. [IndexerName("Chars")]
  152. public char this [int index] {
  153. get {
  154. if (index >= _length || index < 0)
  155. throw new IndexOutOfRangeException();
  156. return _str [index];
  157. }
  158. set {
  159. if (index >= _length || index < 0)
  160. throw new IndexOutOfRangeException();
  161. if (null != _cached_str)
  162. InternalEnsureCapacity (_length);
  163. _str.InternalSetChar (index, value);
  164. }
  165. }
  166. public override string ToString ()
  167. {
  168. if (_length == 0)
  169. return String.Empty;
  170. if (null != _cached_str)
  171. return _cached_str;
  172. // If we only have a half-full buffer we return a new string.
  173. if (_length < (_str.Length >> 1) || (_str.Length > string.LOS_limit && _length <= string.LOS_limit))
  174. {
  175. // use String.SubstringUnchecked instead of String.Substring
  176. // as the former is guaranteed to create a new string object
  177. _cached_str = _str.SubstringUnchecked (0, _length);
  178. return _cached_str;
  179. }
  180. _cached_str = _str;
  181. _str.InternalSetLength(_length);
  182. return _str;
  183. }
  184. public string ToString (int startIndex, int length)
  185. {
  186. // re-ordered to avoid possible integer overflow
  187. if (startIndex < 0 || length < 0 || startIndex > _length - length)
  188. throw new ArgumentOutOfRangeException();
  189. // use String.SubstringUnchecked instead of String.Substring
  190. // as the former is guaranteed to create a new string object
  191. if (startIndex == 0 && length == _length)
  192. return ToString ();
  193. else
  194. return _str.SubstringUnchecked (startIndex, length);
  195. }
  196. public int EnsureCapacity (int capacity)
  197. {
  198. if (capacity < 0)
  199. throw new ArgumentOutOfRangeException ("Capacity must be greater than 0." );
  200. if( capacity <= _str.Length )
  201. return _str.Length;
  202. InternalEnsureCapacity (capacity);
  203. return _str.Length;
  204. }
  205. public bool Equals (StringBuilder sb)
  206. {
  207. if (((object)sb) == null)
  208. return false;
  209. if (_length == sb.Length && _str == sb._str )
  210. return true;
  211. return false;
  212. }
  213. public StringBuilder Remove (int startIndex, int length)
  214. {
  215. // re-ordered to avoid possible integer overflow
  216. if (startIndex < 0 || length < 0 || startIndex > _length - length)
  217. throw new ArgumentOutOfRangeException();
  218. if (null != _cached_str)
  219. InternalEnsureCapacity (_length);
  220. // Copy everything after the 'removed' part to the start
  221. // of the removed part and truncate the sLength
  222. if (_length - (startIndex + length) > 0)
  223. String.CharCopy (_str, startIndex, _str, startIndex + length, _length - (startIndex + length));
  224. _length -= length;
  225. return this;
  226. }
  227. public StringBuilder Replace (char oldChar, char newChar)
  228. {
  229. return Replace( oldChar, newChar, 0, _length);
  230. }
  231. public StringBuilder Replace (char oldChar, char newChar, int startIndex, int count)
  232. {
  233. // re-ordered to avoid possible integer overflow
  234. if (startIndex > _length - count || startIndex < 0 || count < 0)
  235. throw new ArgumentOutOfRangeException();
  236. if (null != _cached_str)
  237. InternalEnsureCapacity (_str.Length);
  238. for (int replaceIterate = startIndex; replaceIterate < startIndex + count; replaceIterate++ ) {
  239. if( _str [replaceIterate] == oldChar )
  240. _str.InternalSetChar (replaceIterate, newChar);
  241. }
  242. return this;
  243. }
  244. public StringBuilder Replace( string oldValue, string newValue ) {
  245. return Replace (oldValue, newValue, 0, _length);
  246. }
  247. public StringBuilder Replace( string oldValue, string newValue, int startIndex, int count )
  248. {
  249. if (oldValue == null)
  250. throw new ArgumentNullException ("The old value cannot be null.");
  251. if (startIndex < 0 || count < 0 || startIndex > _length - count)
  252. throw new ArgumentOutOfRangeException ();
  253. if (oldValue.Length == 0)
  254. throw new ArgumentException ("The old value cannot be zero length.");
  255. string substr = _str.Substring(startIndex, count);
  256. string replace = substr.Replace(oldValue, newValue);
  257. // return early if no oldValue was found
  258. if ((object) replace == (object) substr)
  259. return this;
  260. InternalEnsureCapacity (replace.Length + (_length - count));
  261. // shift end part
  262. if (replace.Length < count)
  263. String.CharCopy (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
  264. else if (replace.Length > count)
  265. String.CharCopyReverse (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
  266. // copy middle part back into _str
  267. String.CharCopy (_str, startIndex, replace, 0, replace.Length);
  268. _length = replace.Length + (_length - count);
  269. return this;
  270. }
  271. /* The Append Methods */
  272. public StringBuilder Append (char[] value)
  273. {
  274. if (value == null)
  275. return this;
  276. int needed_cap = _length + value.Length;
  277. if (null != _cached_str || _str.Length < needed_cap)
  278. InternalEnsureCapacity (needed_cap);
  279. String.CharCopy (_str, _length, value, 0, value.Length);
  280. _length = needed_cap;
  281. return this;
  282. }
  283. public StringBuilder Append (string value)
  284. {
  285. if (value == null)
  286. return this;
  287. if (_length == 0 && value.Length < _maxCapacity && value.Length > _str.Length) {
  288. _length = value.Length;
  289. _str = _cached_str = value;
  290. return this;
  291. }
  292. int needed_cap = _length + value.Length;
  293. if (null != _cached_str || _str.Length < needed_cap)
  294. InternalEnsureCapacity (needed_cap);
  295. String.CharCopy (_str, _length, value, 0, value.Length);
  296. _length = needed_cap;
  297. return this;
  298. }
  299. public StringBuilder Append (bool value) {
  300. return Append (value.ToString());
  301. }
  302. public StringBuilder Append (byte value) {
  303. return Append (value.ToString());
  304. }
  305. public StringBuilder Append (decimal value) {
  306. return Append (value.ToString());
  307. }
  308. public StringBuilder Append (double value) {
  309. return Append (value.ToString());
  310. }
  311. public StringBuilder Append (short value) {
  312. return Append (value.ToString());
  313. }
  314. public StringBuilder Append (int value) {
  315. return Append (value.ToString());
  316. }
  317. public StringBuilder Append (long value) {
  318. return Append (value.ToString());
  319. }
  320. public StringBuilder Append (object value) {
  321. if (value == null)
  322. return this;
  323. return Append (value.ToString());
  324. }
  325. [CLSCompliant(false)]
  326. public StringBuilder Append (sbyte value) {
  327. return Append (value.ToString());
  328. }
  329. public StringBuilder Append (float value) {
  330. return Append (value.ToString());
  331. }
  332. [CLSCompliant(false)]
  333. public StringBuilder Append (ushort value) {
  334. return Append (value.ToString());
  335. }
  336. [CLSCompliant(false)]
  337. public StringBuilder Append (uint value) {
  338. return Append (value.ToString());
  339. }
  340. [CLSCompliant(false)]
  341. public StringBuilder Append (ulong value) {
  342. return Append (value.ToString());
  343. }
  344. public StringBuilder Append (char value)
  345. {
  346. int needed_cap = _length + 1;
  347. if (null != _cached_str || _str.Length < needed_cap)
  348. InternalEnsureCapacity (needed_cap);
  349. _str.InternalSetChar(_length, value);
  350. _length = needed_cap;
  351. return this;
  352. }
  353. public StringBuilder Append (char value, int repeatCount)
  354. {
  355. if( repeatCount < 0 )
  356. throw new ArgumentOutOfRangeException();
  357. InternalEnsureCapacity (_length + repeatCount);
  358. for (int i = 0; i < repeatCount; i++)
  359. _str.InternalSetChar (_length++, value);
  360. return this;
  361. }
  362. public StringBuilder Append( char[] value, int startIndex, int charCount )
  363. {
  364. if (value == null) {
  365. if (!(startIndex == 0 && charCount == 0))
  366. throw new ArgumentNullException ("value");
  367. return this;
  368. }
  369. if ((charCount < 0 || startIndex < 0) || (startIndex > value.Length - charCount))
  370. throw new ArgumentOutOfRangeException();
  371. int needed_cap = _length + charCount;
  372. InternalEnsureCapacity (needed_cap);
  373. String.CharCopy (_str, _length, value, startIndex, charCount);
  374. _length = needed_cap;
  375. return this;
  376. }
  377. public StringBuilder Append (string value, int startIndex, int count)
  378. {
  379. if (value == null) {
  380. if (startIndex != 0 && count != 0)
  381. throw new ArgumentNullException ("value");
  382. return this;
  383. }
  384. if ((count < 0 || startIndex < 0) || (startIndex > value.Length - count))
  385. throw new ArgumentOutOfRangeException();
  386. int needed_cap = _length + count;
  387. if (null != _cached_str || _str.Length < needed_cap)
  388. InternalEnsureCapacity (needed_cap);
  389. String.CharCopy (_str, _length, value, startIndex, count);
  390. _length = needed_cap;
  391. return this;
  392. }
  393. #if NET_4_0
  394. public StringBuilder Clear ()
  395. {
  396. Length = 0;
  397. return this;
  398. }
  399. #endif
  400. [ComVisible (false)]
  401. public StringBuilder AppendLine ()
  402. {
  403. return Append (System.Environment.NewLine);
  404. }
  405. [ComVisible (false)]
  406. public StringBuilder AppendLine (string value)
  407. {
  408. return Append (value).Append (System.Environment.NewLine);
  409. }
  410. public StringBuilder AppendFormat (string format, params object[] args)
  411. {
  412. return AppendFormat (null, format, args);
  413. }
  414. public StringBuilder AppendFormat (IFormatProvider provider,
  415. string format,
  416. params object[] args)
  417. {
  418. String.FormatHelper (this, provider, format, args);
  419. return this;
  420. }
  421. public
  422. StringBuilder AppendFormat (string format, object arg0)
  423. {
  424. return AppendFormat (null, format, new object [] { arg0 });
  425. }
  426. public
  427. StringBuilder AppendFormat (string format, object arg0, object arg1)
  428. {
  429. return AppendFormat (null, format, new object [] { arg0, arg1 });
  430. }
  431. public
  432. StringBuilder AppendFormat (string format, object arg0, object arg1, object arg2)
  433. {
  434. return AppendFormat (null, format, new object [] { arg0, arg1, arg2 });
  435. }
  436. /* The Insert Functions */
  437. public StringBuilder Insert (int index, char[] value)
  438. {
  439. return Insert (index, new string (value));
  440. }
  441. public StringBuilder Insert (int index, string value)
  442. {
  443. if( index > _length || index < 0)
  444. throw new ArgumentOutOfRangeException();
  445. if (value == null || value.Length == 0)
  446. return this;
  447. InternalEnsureCapacity (_length + value.Length);
  448. // Move everything to the right of the insert point across
  449. String.CharCopyReverse (_str, index + value.Length, _str, index, _length - index);
  450. // Copy in stuff from the insert buffer
  451. String.CharCopy (_str, index, value, 0, value.Length);
  452. _length += value.Length;
  453. return this;
  454. }
  455. public StringBuilder Insert( int index, bool value ) {
  456. return Insert (index, value.ToString());
  457. }
  458. public StringBuilder Insert( int index, byte value ) {
  459. return Insert (index, value.ToString());
  460. }
  461. public StringBuilder Insert( int index, char value)
  462. {
  463. if (index > _length || index < 0)
  464. throw new ArgumentOutOfRangeException ("index");
  465. InternalEnsureCapacity (_length + 1);
  466. // Move everything to the right of the insert point across
  467. String.CharCopyReverse (_str, index + 1, _str, index, _length - index);
  468. _str.InternalSetChar (index, value);
  469. _length++;
  470. return this;
  471. }
  472. public StringBuilder Insert( int index, decimal value ) {
  473. return Insert (index, value.ToString());
  474. }
  475. public StringBuilder Insert( int index, double value ) {
  476. return Insert (index, value.ToString());
  477. }
  478. public StringBuilder Insert( int index, short value ) {
  479. return Insert (index, value.ToString());
  480. }
  481. public StringBuilder Insert( int index, int value ) {
  482. return Insert (index, value.ToString());
  483. }
  484. public StringBuilder Insert( int index, long value ) {
  485. return Insert (index, value.ToString());
  486. }
  487. public StringBuilder Insert( int index, object value ) {
  488. return Insert (index, value.ToString());
  489. }
  490. [CLSCompliant(false)]
  491. public StringBuilder Insert( int index, sbyte value ) {
  492. return Insert (index, value.ToString() );
  493. }
  494. public StringBuilder Insert (int index, float value) {
  495. return Insert (index, value.ToString() );
  496. }
  497. [CLSCompliant(false)]
  498. public StringBuilder Insert (int index, ushort value) {
  499. return Insert (index, value.ToString() );
  500. }
  501. [CLSCompliant(false)]
  502. public StringBuilder Insert (int index, uint value) {
  503. return Insert ( index, value.ToString() );
  504. }
  505. [CLSCompliant(false)]
  506. public StringBuilder Insert (int index, ulong value) {
  507. return Insert ( index, value.ToString() );
  508. }
  509. public StringBuilder Insert (int index, string value, int count)
  510. {
  511. // LAMESPEC: The spec says to throw an exception if
  512. // count < 0, while MS throws even for count < 1!
  513. if ( count < 0 )
  514. throw new ArgumentOutOfRangeException();
  515. if (value != null && value != String.Empty)
  516. for (int insertCount = 0; insertCount < count; insertCount++)
  517. Insert( index, value );
  518. return this;
  519. }
  520. public StringBuilder Insert (int index, char [] value, int startIndex, int charCount)
  521. {
  522. if (value == null) {
  523. if (startIndex == 0 && charCount == 0)
  524. return this;
  525. throw new ArgumentNullException ("value");
  526. }
  527. if (charCount < 0 || startIndex < 0 || startIndex > value.Length - charCount)
  528. throw new ArgumentOutOfRangeException ();
  529. return Insert (index, new String (value, startIndex, charCount));
  530. }
  531. private void InternalEnsureCapacity (int size)
  532. {
  533. if (size > _str.Length || (object) _cached_str == (object) _str) {
  534. int capacity = _str.Length;
  535. // Try double buffer, if that doesn't work, set the length as capacity
  536. if (size > capacity) {
  537. // The first time a string is appended, we just set _cached_str
  538. // and _str to it. This allows us to do some optimizations.
  539. // Below, we take this into account.
  540. if ((object) _cached_str == (object) _str && capacity < constDefaultCapacity)
  541. capacity = constDefaultCapacity;
  542. capacity = capacity << 1;
  543. if (size > capacity)
  544. capacity = size;
  545. if (capacity >= Int32.MaxValue || capacity < 0)
  546. capacity = Int32.MaxValue;
  547. if (capacity > _maxCapacity && size <= _maxCapacity)
  548. capacity = _maxCapacity;
  549. if (capacity > _maxCapacity)
  550. throw new ArgumentOutOfRangeException ("size", "capacity was less than the current size.");
  551. }
  552. string tmp = String.InternalAllocateStr (capacity);
  553. if (_length > 0)
  554. String.CharCopy (tmp, 0, _str, 0, _length);
  555. _str = tmp;
  556. }
  557. _cached_str = null;
  558. }
  559. [ComVisible (false)]
  560. public void CopyTo (int sourceIndex, char [] destination, int destinationIndex, int count)
  561. {
  562. if (destination == null)
  563. throw new ArgumentNullException ("destination");
  564. if ((Length - count < sourceIndex) ||
  565. (destination.Length -count < destinationIndex) ||
  566. (sourceIndex < 0 || destinationIndex < 0 || count < 0))
  567. throw new ArgumentOutOfRangeException ();
  568. for (int i = 0; i < count; i++)
  569. destination [destinationIndex+i] = _str [sourceIndex+i];
  570. }
  571. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  572. {
  573. info.AddValue ("m_MaxCapacity", _maxCapacity);
  574. info.AddValue ("Capacity", Capacity);
  575. info.AddValue ("m_StringValue", ToString ());
  576. info.AddValue ("m_currentThread", 0);
  577. }
  578. StringBuilder (SerializationInfo info, StreamingContext context)
  579. {
  580. string s = info.GetString ("m_StringValue");
  581. if (s == null)
  582. s = "";
  583. _length = s.Length;
  584. _str = _cached_str = s;
  585. _maxCapacity = info.GetInt32 ("m_MaxCapacity");
  586. if (_maxCapacity < 0)
  587. _maxCapacity = Int32.MaxValue;
  588. Capacity = info.GetInt32 ("Capacity");
  589. }
  590. }
  591. }