Strings.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. #pragma warning disable
  3. using System;
  4. using System.Text;
  5. namespace BestHTTP.SecureProtocol.Org.BouncyCastle.Utilities
  6. {
  7. /// <summary> General string utilities.</summary>
  8. public abstract class Strings
  9. {
  10. public static string ToUpperCase(string original)
  11. {
  12. bool changed = false;
  13. char[] chars = original.ToCharArray();
  14. for (int i = 0; i != chars.Length; i++)
  15. {
  16. char ch = chars[i];
  17. if ('a' <= ch && 'z' >= ch)
  18. {
  19. changed = true;
  20. chars[i] = (char)(ch - 'a' + 'A');
  21. }
  22. }
  23. if (changed)
  24. {
  25. return new String(chars);
  26. }
  27. return original;
  28. }
  29. internal static bool IsOneOf(string s, params string[] candidates)
  30. {
  31. foreach (string candidate in candidates)
  32. {
  33. if (s == candidate)
  34. return true;
  35. }
  36. return false;
  37. }
  38. public static string FromByteArray(
  39. byte[] bs)
  40. {
  41. char[] cs = new char[bs.Length];
  42. for (int i = 0; i < cs.Length; ++i)
  43. {
  44. cs[i] = Convert.ToChar(bs[i]);
  45. }
  46. return new string(cs);
  47. }
  48. public static byte[] ToByteArray(
  49. char[] cs)
  50. {
  51. byte[] bs = new byte[cs.Length];
  52. for (int i = 0; i < bs.Length; ++i)
  53. {
  54. bs[i] = Convert.ToByte(cs[i]);
  55. }
  56. return bs;
  57. }
  58. public static byte[] ToByteArray(
  59. string s)
  60. {
  61. byte[] bs = new byte[s.Length];
  62. for (int i = 0; i < bs.Length; ++i)
  63. {
  64. bs[i] = Convert.ToByte(s[i]);
  65. }
  66. return bs;
  67. }
  68. public static string FromAsciiByteArray(
  69. byte[] bytes)
  70. {
  71. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  72. // TODO Check for non-ASCII bytes in input?
  73. return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  74. #else
  75. return Encoding.ASCII.GetString(bytes, 0, bytes.Length);
  76. #endif
  77. }
  78. public static byte[] ToAsciiByteArray(
  79. char[] cs)
  80. {
  81. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  82. // TODO Check for non-ASCII characters in input?
  83. return Encoding.UTF8.GetBytes(cs);
  84. #else
  85. return Encoding.ASCII.GetBytes(cs);
  86. #endif
  87. }
  88. public static byte[] ToAsciiByteArray(
  89. string s)
  90. {
  91. #if SILVERLIGHT || PORTABLE || NETFX_CORE
  92. // TODO Check for non-ASCII characters in input?
  93. return Encoding.UTF8.GetBytes(s);
  94. #else
  95. return Encoding.ASCII.GetBytes(s);
  96. #endif
  97. }
  98. public static string FromUtf8ByteArray(
  99. byte[] bytes)
  100. {
  101. return Encoding.UTF8.GetString(bytes, 0, bytes.Length);
  102. }
  103. public static byte[] ToUtf8ByteArray(
  104. char[] cs)
  105. {
  106. return Encoding.UTF8.GetBytes(cs);
  107. }
  108. public static byte[] ToUtf8ByteArray(
  109. string s)
  110. {
  111. return Encoding.UTF8.GetBytes(s);
  112. }
  113. }
  114. }
  115. #pragma warning restore
  116. #endif