StringUtilities.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 "Header.h"
  30. #include "Types.h"
  31. #include "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 integer value using the FNV algorithm.
  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 Integer hash of the data.
  57. static Hash FNVHash(const char* data, int length = -1);
  58. /// Converts a character array in UTF-8 encoding to a vector of words. The UCS-2 words will be encoded as
  59. /// either big- or little-endian, depending on the host processor.
  60. /// @param[in] input Input string in UTF-8 encoding.
  61. /// @param[out] output Output vector of UCS-2 characters.
  62. /// @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).
  63. static bool UTF8toUCS2(const String& input, std::vector< word >& output);
  64. /// Converts a vector of words in UCS-2 encoding into a character array in UTF-8 encoding. This
  65. /// function assumes the endianness of the input words to be the same as the host processor.
  66. /// @param[in] input Input vector in UCS-2 encoding.
  67. /// @param[out] output Output string in UTF-8 encoding.
  68. /// @return True if the conversion went successfully, false if not.
  69. static bool UCS2toUTF8(const std::vector< word >& input, String& output);
  70. /// Converts an array of words in UCS-2 encoding into a character array in UTF-8 encoding. This
  71. /// function assumes the endianness of the input words to be the same as the host processor.
  72. /// @param[in] input Input array of words in UCS-2 encoding.
  73. /// @param[in] input_size Length of the input array.
  74. /// @param[out] output Output string in UTF-8 encoding.
  75. /// @return True if the conversion went successfully, false if not.
  76. static bool UCS2toUTF8(const word* input, size_t input_size, String& output);
  77. /// Checks if a given value is a whitespace character.
  78. /// @param[in] x The character to evaluate.
  79. /// @return True if the character is whitespace, false otherwise.
  80. template < typename CharacterType >
  81. static bool IsWhitespace(CharacterType x)
  82. {
  83. return (x == '\r' || x == '\n' || x == ' ' || x == '\t');
  84. }
  85. /// Strip whitespace characters from the beginning and end of a string.
  86. /// @param[in] string The string to trim.
  87. /// @return The stripped string.
  88. static String StripWhitespace(const String& string);
  89. /// Operator for STL containers using strings.
  90. struct ROCKETCORE_API StringComparei
  91. {
  92. bool operator()(const String& lhs, const String& rhs) const;
  93. };
  94. };
  95. }
  96. }
  97. #endif