StringBuilder.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773
  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. #if NET_2_0
  44. [ComVisible (true)]
  45. #endif
  46. [MonoTODO ("Serialization format not compatible with .NET")]
  47. public sealed class StringBuilder
  48. #if NET_2_0
  49. : ISerializable
  50. #endif
  51. {
  52. private int _length;
  53. private string _str;
  54. private string _cached_str;
  55. private int _maxCapacity;
  56. private const int constDefaultCapacity = 16;
  57. public StringBuilder(string value, int startIndex, int length, int capacity)
  58. : this (value, startIndex, length, capacity, Int32.MaxValue)
  59. {
  60. }
  61. private StringBuilder(string value, int startIndex, int length, int capacity, int maxCapacity)
  62. {
  63. // first, check the parameters and throw appropriate exceptions if needed
  64. if (null == value)
  65. value = "";
  66. // make sure startIndex is zero or positive
  67. if (startIndex < 0)
  68. throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex cannot be less than zero.");
  69. // make sure length is zero or positive
  70. if(length < 0)
  71. throw new System.ArgumentOutOfRangeException ("length", length, "Length cannot be less than zero.");
  72. if (capacity < 0)
  73. throw new System.ArgumentOutOfRangeException ("capacity", capacity, "capacity must be greater than zero.");
  74. if (maxCapacity < 1)
  75. throw new System.ArgumentOutOfRangeException ("maxCapacity", "maxCapacity is less than one.");
  76. if (capacity > maxCapacity)
  77. throw new System.ArgumentOutOfRangeException ("capacity", "Capacity exceeds maximum capacity.");
  78. // make sure startIndex and length give a valid substring of value
  79. // re-ordered to avoid possible integer overflow
  80. if (startIndex > value.Length - length)
  81. throw new System.ArgumentOutOfRangeException ("startIndex", startIndex, "StartIndex and length must refer to a location within the string.");
  82. if (capacity == 0) {
  83. if (maxCapacity > constDefaultCapacity)
  84. capacity = constDefaultCapacity;
  85. else
  86. _str = _cached_str = String.Empty;
  87. }
  88. _maxCapacity = maxCapacity;
  89. if (_str == null)
  90. _str = String.InternalAllocateStr ((length > capacity) ? length : capacity);
  91. if (length > 0)
  92. String.CharCopy (_str, 0, value, startIndex, length);
  93. _length = length;
  94. }
  95. public StringBuilder () : this (null) {}
  96. public StringBuilder(int capacity) : this (String.Empty, 0, 0, capacity) {}
  97. public StringBuilder(int capacity, int maxCapacity) : this (String.Empty, 0, 0, capacity, maxCapacity) { }
  98. public StringBuilder (string value)
  99. {
  100. /*
  101. * This is an optimization to avoid allocating the internal string
  102. * until the first Append () call.
  103. * The runtime pinvoke marshalling code needs to be aware of this.
  104. */
  105. if (null == value)
  106. value = "";
  107. _length = value.Length;
  108. _str = _cached_str = value;
  109. _maxCapacity = Int32.MaxValue;
  110. }
  111. public StringBuilder( string value, int capacity) : this(value == null ? "" : value, 0, value == null ? 0 : value.Length, capacity) {}
  112. public int MaxCapacity {
  113. get {
  114. return _maxCapacity;
  115. }
  116. }
  117. public int Capacity {
  118. get {
  119. if (_str.Length == 0)
  120. return Math.Min (_maxCapacity, constDefaultCapacity);
  121. return _str.Length;
  122. }
  123. set {
  124. if (value < _length)
  125. throw new ArgumentException( "Capacity must be larger than length" );
  126. if (value > _maxCapacity)
  127. throw new ArgumentOutOfRangeException ("value", "Should be less than or equal to MaxCapacity");
  128. InternalEnsureCapacity(value);
  129. }
  130. }
  131. public int Length {
  132. get {
  133. return _length;
  134. }
  135. set {
  136. if( value < 0 || value > _maxCapacity)
  137. throw new ArgumentOutOfRangeException();
  138. if (value == _length)
  139. return;
  140. if (value < _length) {
  141. // LAMESPEC: The spec is unclear as to what to do
  142. // with the capacity when truncating the string.
  143. // Do as MS, keep the capacity
  144. // Make sure that we invalidate any cached string.
  145. InternalEnsureCapacity (value);
  146. _length = value;
  147. } else {
  148. // Expand the capacity to the new length and
  149. // pad the string with NULL characters.
  150. Append('\0', value - _length);
  151. }
  152. }
  153. }
  154. [IndexerName("Chars")]
  155. public char this [int index] {
  156. get {
  157. if (index >= _length || index < 0)
  158. throw new IndexOutOfRangeException();
  159. return _str [index];
  160. }
  161. set {
  162. if (index >= _length || index < 0)
  163. throw new IndexOutOfRangeException();
  164. if (null != _cached_str)
  165. InternalEnsureCapacity (_length);
  166. _str.InternalSetChar (index, value);
  167. }
  168. }
  169. public override string ToString ()
  170. {
  171. if (_length == 0)
  172. return String.Empty;
  173. if (null != _cached_str)
  174. return _cached_str;
  175. // If we only have a half-full buffer we return a new string.
  176. if (_length < (_str.Length >> 1))
  177. {
  178. // use String.SubstringUnchecked instead of String.Substring
  179. // as the former is guaranteed to create a new string object
  180. _cached_str = _str.SubstringUnchecked (0, _length);
  181. return _cached_str;
  182. }
  183. _cached_str = _str;
  184. _str.InternalSetLength(_length);
  185. return _str;
  186. }
  187. public string ToString (int startIndex, int length)
  188. {
  189. // re-ordered to avoid possible integer overflow
  190. if (startIndex < 0 || length < 0 || startIndex > _length - length)
  191. throw new ArgumentOutOfRangeException();
  192. // use String.SubstringUnchecked instead of String.Substring
  193. // as the former is guaranteed to create a new string object
  194. if (startIndex == 0 && length == _length)
  195. return ToString ();
  196. else
  197. return _str.SubstringUnchecked (startIndex, length);
  198. }
  199. public int EnsureCapacity (int capacity)
  200. {
  201. if (capacity < 0)
  202. throw new ArgumentOutOfRangeException ("Capacity must be greater than 0." );
  203. if( capacity <= _str.Length )
  204. return _str.Length;
  205. InternalEnsureCapacity (capacity);
  206. return _str.Length;
  207. }
  208. public bool Equals (StringBuilder sb)
  209. {
  210. if (((object)sb) == null)
  211. return false;
  212. if (_length == sb.Length && _str == sb._str )
  213. return true;
  214. return false;
  215. }
  216. public StringBuilder Remove (int startIndex, int length)
  217. {
  218. // re-ordered to avoid possible integer overflow
  219. if (startIndex < 0 || length < 0 || startIndex > _length - length)
  220. throw new ArgumentOutOfRangeException();
  221. if (null != _cached_str)
  222. InternalEnsureCapacity (_length);
  223. // Copy everything after the 'removed' part to the start
  224. // of the removed part and truncate the sLength
  225. if (_length - (startIndex + length) > 0)
  226. String.CharCopy (_str, startIndex, _str, startIndex + length, _length - (startIndex + length));
  227. _length -= length;
  228. return this;
  229. }
  230. public StringBuilder Replace (char oldChar, char newChar)
  231. {
  232. return Replace( oldChar, newChar, 0, _length);
  233. }
  234. public StringBuilder Replace (char oldChar, char newChar, int startIndex, int count)
  235. {
  236. // re-ordered to avoid possible integer overflow
  237. if (startIndex > _length - count || startIndex < 0 || count < 0)
  238. throw new ArgumentOutOfRangeException();
  239. if (null != _cached_str)
  240. InternalEnsureCapacity (_str.Length);
  241. for (int replaceIterate = startIndex; replaceIterate < startIndex + count; replaceIterate++ ) {
  242. if( _str [replaceIterate] == oldChar )
  243. _str.InternalSetChar (replaceIterate, newChar);
  244. }
  245. return this;
  246. }
  247. public StringBuilder Replace( string oldValue, string newValue ) {
  248. return Replace (oldValue, newValue, 0, _length);
  249. }
  250. public StringBuilder Replace( string oldValue, string newValue, int startIndex, int count )
  251. {
  252. if (oldValue == null)
  253. throw new ArgumentNullException ("The old value cannot be null.");
  254. if (startIndex < 0 || count < 0 || startIndex > _length - count)
  255. throw new ArgumentOutOfRangeException ();
  256. if (oldValue.Length == 0)
  257. throw new ArgumentException ("The old value cannot be zero length.");
  258. string substr = _str.Substring(startIndex, count);
  259. string replace = substr.Replace(oldValue, newValue);
  260. // return early if no oldValue was found
  261. if ((object) replace == (object) substr)
  262. return this;
  263. InternalEnsureCapacity (replace.Length + (_length - count));
  264. // shift end part
  265. if (replace.Length < count)
  266. String.CharCopy (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
  267. else if (replace.Length > count)
  268. String.CharCopyReverse (_str, startIndex + replace.Length, _str, startIndex + count, _length - startIndex - count);
  269. // copy middle part back into _str
  270. String.CharCopy (_str, startIndex, replace, 0, replace.Length);
  271. _length = replace.Length + (_length - count);
  272. return this;
  273. }
  274. /* The Append Methods */
  275. public StringBuilder Append (char[] value)
  276. {
  277. if (value == null)
  278. return this;
  279. int needed_cap = _length + value.Length;
  280. if (null != _cached_str || _str.Length < needed_cap)
  281. InternalEnsureCapacity (needed_cap);
  282. String.CharCopy (_str, _length, value, 0, value.Length);
  283. _length = needed_cap;
  284. return this;
  285. }
  286. public StringBuilder Append (string value)
  287. {
  288. if (value == null)
  289. return this;
  290. if (_length == 0 && value.Length < _maxCapacity && value.Length > _str.Length) {
  291. _length = value.Length;
  292. _str = _cached_str = value;
  293. return this;
  294. }
  295. int needed_cap = _length + value.Length;
  296. if (null != _cached_str || _str.Length < needed_cap)
  297. InternalEnsureCapacity (needed_cap);
  298. String.CharCopy (_str, _length, value, 0, value.Length);
  299. _length = needed_cap;
  300. return this;
  301. }
  302. public StringBuilder Append (bool value) {
  303. return Append (value.ToString());
  304. }
  305. public StringBuilder Append (byte value) {
  306. return Append (value.ToString());
  307. }
  308. public StringBuilder Append (decimal value) {
  309. return Append (value.ToString());
  310. }
  311. public StringBuilder Append (double value) {
  312. return Append (value.ToString());
  313. }
  314. public StringBuilder Append (short value) {
  315. return Append (value.ToString());
  316. }
  317. public StringBuilder Append (int value) {
  318. return Append (value.ToString());
  319. }
  320. public StringBuilder Append (long value) {
  321. return Append (value.ToString());
  322. }
  323. public StringBuilder Append (object value) {
  324. if (value == null)
  325. return this;
  326. return Append (value.ToString());
  327. }
  328. [CLSCompliant(false)]
  329. public StringBuilder Append (sbyte value) {
  330. return Append (value.ToString());
  331. }
  332. public StringBuilder Append (float value) {
  333. return Append (value.ToString());
  334. }
  335. [CLSCompliant(false)]
  336. public StringBuilder Append (ushort value) {
  337. return Append (value.ToString());
  338. }
  339. [CLSCompliant(false)]
  340. public StringBuilder Append (uint value) {
  341. return Append (value.ToString());
  342. }
  343. [CLSCompliant(false)]
  344. public StringBuilder Append (ulong value) {
  345. return Append (value.ToString());
  346. }
  347. public StringBuilder Append (char value)
  348. {
  349. int needed_cap = _length + 1;
  350. if (null != _cached_str || _str.Length < needed_cap)
  351. InternalEnsureCapacity (needed_cap);
  352. _str.InternalSetChar(_length, value);
  353. _length = needed_cap;
  354. return this;
  355. }
  356. public StringBuilder Append (char value, int repeatCount)
  357. {
  358. if( repeatCount < 0 )
  359. throw new ArgumentOutOfRangeException();
  360. InternalEnsureCapacity (_length + repeatCount);
  361. for (int i = 0; i < repeatCount; i++)
  362. _str.InternalSetChar (_length++, value);
  363. return this;
  364. }
  365. public StringBuilder Append( char[] value, int startIndex, int charCount )
  366. {
  367. if (value == null) {
  368. if (!(startIndex == 0 && charCount == 0))
  369. throw new ArgumentNullException ("value");
  370. return this;
  371. }
  372. if ((charCount < 0 || startIndex < 0) || (startIndex > value.Length - charCount))
  373. throw new ArgumentOutOfRangeException();
  374. int needed_cap = _length + charCount;
  375. InternalEnsureCapacity (needed_cap);
  376. String.CharCopy (_str, _length, value, startIndex, charCount);
  377. _length = needed_cap;
  378. return this;
  379. }
  380. public StringBuilder Append (string value, int startIndex, int count)
  381. {
  382. if (value == null) {
  383. if (startIndex != 0 && count != 0)
  384. throw new ArgumentNullException ("value");
  385. return this;
  386. }
  387. if ((count < 0 || startIndex < 0) || (startIndex > value.Length - count))
  388. throw new ArgumentOutOfRangeException();
  389. int needed_cap = _length + count;
  390. if (null != _cached_str || _str.Length < needed_cap)
  391. InternalEnsureCapacity (needed_cap);
  392. String.CharCopy (_str, _length, value, startIndex, count);
  393. _length = needed_cap;
  394. return this;
  395. }
  396. #if NET_2_0
  397. [ComVisible (false)]
  398. public StringBuilder AppendLine ()
  399. {
  400. return Append (System.Environment.NewLine);
  401. }
  402. [ComVisible (false)]
  403. public StringBuilder AppendLine (string value)
  404. {
  405. return Append (value).Append (System.Environment.NewLine);
  406. }
  407. #endif
  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 NET_2_1 && !MONOTOUCH
  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 NET_2_1 && !MONOTOUCH
  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 NET_2_1 && !MONOTOUCH
  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. #if NET_2_0
  570. [ComVisible (false)]
  571. public void CopyTo (int sourceIndex, char [] destination, int destinationIndex, int count)
  572. {
  573. if (destination == null)
  574. throw new ArgumentNullException ("destination");
  575. if ((Length - count < sourceIndex) ||
  576. (destination.Length -count < destinationIndex) ||
  577. (sourceIndex < 0 || destinationIndex < 0 || count < 0))
  578. throw new ArgumentOutOfRangeException ();
  579. for (int i = 0; i < count; i++)
  580. destination [destinationIndex+i] = _str [sourceIndex+i];
  581. }
  582. void ISerializable.GetObjectData (SerializationInfo info, StreamingContext context)
  583. {
  584. info.AddValue ("m_MaxCapacity", _maxCapacity);
  585. info.AddValue ("Capacity", Capacity);
  586. info.AddValue ("m_StringValue", ToString ());
  587. info.AddValue ("m_currentThread", 0);
  588. }
  589. StringBuilder (SerializationInfo info, StreamingContext context)
  590. {
  591. string s = info.GetString ("m_StringValue");
  592. if (s == null)
  593. s = "";
  594. _length = s.Length;
  595. _str = _cached_str = s;
  596. _maxCapacity = info.GetInt32 ("m_MaxCapacity");
  597. if (_maxCapacity < 0)
  598. _maxCapacity = Int32.MaxValue;
  599. Capacity = info.GetInt32 ("Capacity");
  600. }
  601. #endif
  602. }
  603. }