StringBuilder.cs 21 KB

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