StringUtilities.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #ifndef ROCKETCORESTRINGUTILITIES_H
  28. #define ROCKETCORESTRINGUTILITIES_H
  29. #include <Rocket/Core/Header.h>
  30. #include <Rocket/Core/Types.h>
  31. #include <Rocket/Core/String.h>
  32. #include <stdarg.h>
  33. namespace Rocket {
  34. namespace Core {
  35. /**
  36. Helper functions for string manipulation.
  37. @author Lloyd Weehuizen
  38. */
  39. class ROCKETCORE_API StringUtilities
  40. {
  41. public:
  42. /// Expands character-delimited list of values in a single string to a whitespace-trimmed list
  43. /// of values.
  44. /// @param[out] string_list Resulting list of values.
  45. /// @param[in] string String to expand.
  46. /// @param[in] delimiter Delimiter found between entries in the string list.
  47. static void ExpandString(StringList& string_list, const String& string, const char delimiter = ',');
  48. /// Joins a list of string values into a single string separated by a character delimiter.
  49. /// @param[out] string Resulting concatenated string.
  50. /// @param[in] string_list Input list of string values.
  51. /// @param[in] delimiter Delimiter to insert between the individual values.
  52. static void JoinString(String& string, const StringList& string_list, const char delimiter = ',');
  53. /// Hashes a string of data to an 32-character MD5 value.
  54. /// @param[in] data Data to hash.
  55. /// @param[in] length Length of the string to hash. If this is -1, the data will be interpreted as a C string.
  56. /// @return MD5 hash of the data.
  57. static String MD5Hash(const char* data, int length = -1);
  58. /// Hashes a string of data to an integer value using the FNV algorithm.
  59. /// @param[in] data Data to hash.
  60. /// @param[in] length Length of the string to hash. If this is -1, the data will be interpreted as a C string.
  61. /// @return Integer hash of the data.
  62. static Hash FNVHash(const char* data, int length = -1);
  63. /// Encodes a string with URL-encoding.
  64. /// @param[in] input Input ASCII string to encode.
  65. /// @param[in] input_length Length of the input string.
  66. /// @param[out] output Output URL-encoded string.
  67. /// @return True if the encoding was successful, false otherwise.
  68. static bool URLEncode(const char* input, size_t input_length, String& output);
  69. /// Decodes a URL-encoded string.
  70. /// @param[in] input Input URL-encoded string.
  71. /// @param[out] output Output buffer for the decoded characters.
  72. /// @param[in] Length of the output buffer.
  73. /// @return False if the decoding failed or the output buffer was too short, true otherwise.
  74. static bool URLDecode(const String& input, char* output, size_t output_length);
  75. /// Encodes a string with base64-encoding.
  76. /// @param[in] input Input ASCII string to encode.
  77. /// @param[in] input_length Length of the input string.
  78. /// @param[out] output Output base-64 encoded string.
  79. /// @return True if the encoding was successful, false otherwise.
  80. static bool Base64Encode(const char* input, size_t input_length, String& output);
  81. /// Decodes a base64-encoded string.
  82. /// @param[in] input Input base-64 encoded string.
  83. /// @param[out] output Output buffer for the decoded characters.
  84. /// @param[in] Length of the output buffer.
  85. /// @return False if the decoding failed or the output buffer was too short, true otherwise.
  86. static bool Base64Decode(const String& input, char* output, size_t output_length);
  87. /// Converts a character array in UTF-8 encoding to a vector of words. The UCS-2 words will be encoded as
  88. /// either big- or little-endian, depending on the host processor.
  89. /// @param[in] input Input string in UTF-8 encoding.
  90. /// @param[out] output Output vector of UCS-2 characters.
  91. /// @return True if the conversion went successfully, false if any characters had to be skipped (this will occur if they can't fit into UCS-2).
  92. static bool UTF8toUCS2(const String& input, std::vector< word >& output);
  93. /// Converts a vector of words in UCS-2 encoding into a character array in UTF-8 encoding. This
  94. /// function assumes the endianness of the input words to be the same as the host processor.
  95. /// @param[in] input Input vector in UCS-2 encoding.
  96. /// @param[out] output Output string in UTF-8 encoding.
  97. /// @return True if the conversion went successfully, false if not.
  98. static bool UCS2toUTF8(const std::vector< word >& input, String& output);
  99. /// Converts an array of words in UCS-2 encoding into a character array in UTF-8 encoding. This
  100. /// function assumes the endianness of the input words to be the same as the host processor.
  101. /// @param[in] input Input array of words in UCS-2 encoding.
  102. /// @param[in] input_size Length of the input array.
  103. /// @param[out] output Output string in UTF-8 encoding.
  104. /// @return True if the conversion went successfully, false if not.
  105. static bool UCS2toUTF8(const word* input, size_t input_size, String& output);
  106. /// Checks if a given value is a whitespace character.
  107. /// @param[in] x The character to evaluate.
  108. /// @return True if the character is whitespace, false otherwise.
  109. template < typename CharacterType >
  110. static bool IsWhitespace(CharacterType x)
  111. {
  112. return (x == '\r' || x == '\n' || x == ' ' || x == '\t');
  113. }
  114. /// Strip whitespace characters from the beginning and end of a string.
  115. /// @param[in] string The string to trim.
  116. /// @return The stripped string.
  117. static String StripWhitespace(const String& string);
  118. struct ROCKETCORE_API ArgumentState
  119. {
  120. ArgumentState();
  121. int index;
  122. char option;
  123. const char* argument;
  124. bool display_errors;
  125. };
  126. /// getopt program argument processing.
  127. static int GetOpt(int nargc, char* nargv[], char* optstring, ArgumentState& arg_state);
  128. /// Operator for STL containers using strings.
  129. struct ROCKETCORE_API StringComparei
  130. {
  131. bool operator()(const String& lhs, const String& rhs) const;
  132. };
  133. };
  134. }
  135. }
  136. #endif