2
0

StringBuilderExtensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //
  2. // StringBuilderExtensions.cs
  3. //
  4. // Author:
  5. // Marek Habersack <[email protected]>
  6. //
  7. // (C) 2008 Novell, Inc. http://novell.com/
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Text;
  31. namespace System.Web.Script.Serialization
  32. {
  33. internal static class StringBuilderExtensions
  34. {
  35. static void CheckCount (StringBuilder sb, int maxCount)
  36. {
  37. if (sb.Length > maxCount)
  38. throw new InvalidOperationException ("Maximum length exceeded.");
  39. }
  40. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, char[] value)
  41. {
  42. StringBuilder ret = sb.Append (value);
  43. CheckCount (sb, maxCount);
  44. return ret;
  45. }
  46. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, string value)
  47. {
  48. StringBuilder ret = sb.Append (value);
  49. CheckCount (sb, maxCount);
  50. return ret;
  51. }
  52. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, bool value)
  53. {
  54. StringBuilder ret = sb.Append (value);
  55. CheckCount (sb, maxCount);
  56. return ret;
  57. }
  58. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, byte value)
  59. {
  60. StringBuilder ret = sb.Append (value);
  61. CheckCount (sb, maxCount);
  62. return ret;
  63. }
  64. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, decimal value)
  65. {
  66. StringBuilder ret = sb.Append (value);
  67. CheckCount (sb, maxCount);
  68. return ret;
  69. }
  70. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, double value)
  71. {
  72. StringBuilder ret = sb.Append (value);
  73. CheckCount (sb, maxCount);
  74. return ret;
  75. }
  76. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, short value)
  77. {
  78. StringBuilder ret = sb.Append (value);
  79. CheckCount (sb, maxCount);
  80. return ret;
  81. }
  82. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, int value)
  83. {
  84. StringBuilder ret = sb.Append (value);
  85. CheckCount (sb, maxCount);
  86. return ret;
  87. }
  88. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, long value)
  89. {
  90. StringBuilder ret = sb.Append (value);
  91. CheckCount (sb, maxCount);
  92. return ret;
  93. }
  94. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, object value)
  95. {
  96. StringBuilder ret = sb.Append (value);
  97. CheckCount (sb, maxCount);
  98. return ret;
  99. }
  100. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, sbyte value)
  101. {
  102. StringBuilder ret = sb.Append (value);
  103. CheckCount (sb, maxCount);
  104. return ret;
  105. }
  106. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, float value)
  107. {
  108. StringBuilder ret = sb.Append (value);
  109. CheckCount (sb, maxCount);
  110. return ret;
  111. }
  112. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, ushort value)
  113. {
  114. StringBuilder ret = sb.Append (value);
  115. CheckCount (sb, maxCount);
  116. return ret;
  117. }
  118. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, uint value)
  119. {
  120. StringBuilder ret = sb.Append (value);
  121. CheckCount (sb, maxCount);
  122. return ret;
  123. }
  124. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, ulong value)
  125. {
  126. StringBuilder ret = sb.Append (value);
  127. CheckCount (sb, maxCount);
  128. return ret;
  129. }
  130. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, char value)
  131. {
  132. StringBuilder ret = sb.Append (value);
  133. CheckCount (sb, maxCount);
  134. return ret;
  135. }
  136. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, char value, int repeatCount)
  137. {
  138. StringBuilder ret = sb.Append (value, repeatCount);
  139. CheckCount (sb, maxCount);
  140. return ret;
  141. }
  142. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, char[] value, int startIndex, int charCount )
  143. {
  144. StringBuilder ret = sb.Append (value, startIndex, charCount);
  145. CheckCount (sb, maxCount);
  146. return ret;
  147. }
  148. public static StringBuilder AppendCount (this StringBuilder sb, int maxCount, string value, int startIndex, int count)
  149. {
  150. StringBuilder ret = sb.Append (value, startIndex, count);
  151. CheckCount (sb, maxCount);
  152. return ret;
  153. }
  154. }
  155. }