PageRenderTime 59ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/MAIN-OLD/Source/Facebook/Utility/StringHelper.cs

#
C# | 128 lines | 87 code | 10 blank | 31 comment | 12 complexity | 7b68eed7f44b79361b2d8db912cd18a7 MD5 | raw file
  1. using System.Collections.Generic;
  2. using System.ComponentModel;
  3. using System.Text;
  4. using facebook.Schema;
  5. namespace facebook.Utility
  6. {
  7. internal sealed class StringHelper
  8. {
  9. private StringHelper()
  10. {
  11. }
  12. /// <summary>
  13. /// Convert a collection of strings to a comma separated list.
  14. /// </summary>
  15. /// <param name="collection">The collection to convert to a comma separated list.</param>
  16. /// <returns>comma separated string.</returns>
  17. internal static string ConvertToCommaSeparated(IList<string> collection)
  18. {
  19. ///
  20. /// assumed that the average string length is 10 and double the buffer multiplying by 2
  21. /// if this does not fit in your case, please change the values
  22. ///
  23. int preAllocation = collection.Count * 10 * 2;
  24. var sb = new StringBuilder(preAllocation);
  25. int i = 0;
  26. foreach (string key in collection)
  27. {
  28. sb.Append(key);
  29. if (i < collection.Count - 1)
  30. sb.Append(",");
  31. i++;
  32. }
  33. return sb.ToString();
  34. }
  35. /// <summary>
  36. /// Convert a collection of strings to a comma separated list.
  37. /// </summary>
  38. /// <param name="collection">The collection to convert to a comma separated list.</param>
  39. /// <returns>comma separated string.</returns>
  40. internal static string ConvertToCommaSeparated(IList<int?> collection)
  41. {
  42. ///
  43. /// assumed that the average string length is 10 and double the buffer multiplying by 2
  44. /// if this does not fit in your case, please change the values
  45. ///
  46. int preAllocation = collection.Count * 10 * 2;
  47. var sb = new StringBuilder(preAllocation);
  48. int i = 0;
  49. foreach (int? key in collection)
  50. {
  51. sb.Append(key.ToString());
  52. if (i < collection.Count - 1)
  53. sb.Append(",");
  54. i++;
  55. }
  56. return sb.ToString();
  57. }
  58. /// <summary>
  59. /// Convert a collection of strings to a comma separated list.
  60. /// </summary>
  61. /// <param name="collection">The collection to convert to a comma separated list.</param>
  62. /// <returns>comma separated string.</returns>
  63. internal static string ConvertToCommaSeparated(IList<int> collection)
  64. {
  65. ///
  66. /// assumed that the average string length is 10 and double the buffer multiplying by 2
  67. /// if this does not fit in your case, please change the values
  68. ///
  69. int preAllocation = collection.Count * 10 * 2;
  70. var sb = new StringBuilder(preAllocation);
  71. int i = 0;
  72. foreach (int key in collection)
  73. {
  74. sb.Append(key.ToString());
  75. if (i < collection.Count - 1)
  76. sb.Append(",");
  77. i++;
  78. }
  79. return sb.ToString();
  80. }
  81. internal static string ConvertToCommaSeparated(IList<long> collection)
  82. {
  83. ///
  84. /// assumed that the average string length is 10 and double the buffer multiplying by 2
  85. /// if this does not fit in your case, please change the values
  86. ///
  87. int preAllocation = collection.Count * 10 * 2;
  88. var sb = new StringBuilder(preAllocation);
  89. int i = 0;
  90. foreach (long key in collection)
  91. {
  92. sb.Append(key.ToString());
  93. if (i < collection.Count - 1)
  94. sb.Append(",");
  95. i++;
  96. }
  97. return sb.ToString();
  98. }
  99. public static string StripNonValidXMLCharacters(string s)
  100. {
  101. StringBuilder _validXML = new StringBuilder(s.Length, s.Length); // Used to hold the output.
  102. char current; // Used to reference the current character.
  103. char[] charArray = s.ToCharArray();
  104. if (string.IsNullOrEmpty(s)) return string.Empty; // vacancy test.
  105. for (int i = 0; i < charArray.Length; i++)
  106. {
  107. current = charArray[i]; // NOTE: No IndexOutOfBoundsException caught here; it should not happen.
  108. if ((current == 0x9) ||
  109. (current == 0xA) ||
  110. (current == 0xD) ||
  111. ((current >= 0x20) && (current <= 0xD7FF)) ||
  112. ((current >= 0xE000) && (current <= 0xFFFD)))
  113. _validXML.Append(current);
  114. }
  115. return _validXML.ToString();
  116. }
  117. }
  118. }