StringUtilities.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. namespace Rocket {
  33. namespace Core {
  34. /**
  35. Helper functions for string manipulation.
  36. @author Lloyd Weehuizen
  37. */
  38. class ROCKETCORE_API StringUtilities
  39. {
  40. public:
  41. /// Expands character-delimited list of values in a single string to a whitespace-trimmed list
  42. /// of values.
  43. /// @param[out] string_list Resulting list of values.
  44. /// @param[in] string String to expand.
  45. /// @param[in] delimiter Delimiter found between entries in the string list.
  46. static void ExpandString(StringList& string_list, const String& string, const char delimiter = ',');
  47. /// Joins a list of string values into a single string separated by a character delimiter.
  48. /// @param[out] string Resulting concatenated string.
  49. /// @param[in] string_list Input list of string values.
  50. /// @param[in] delimiter Delimiter to insert between the individual values.
  51. static void JoinString(String& string, const StringList& string_list, const char delimiter = ',');
  52. /// Hashes a string of data to an integer value using the FNV algorithm.
  53. /// @param[in] data Data to hash.
  54. /// @param[in] length Length of the string to hash. If this is -1, the data will be interpreted as a C string.
  55. /// @return Integer hash of the data.
  56. static Hash FNVHash(const char* data, int length = -1);
  57. /// Converts a character array in UTF-8 encoding to a vector of words. The UCS-2 words will be encoded as
  58. /// either big- or little-endian, depending on the host processor.
  59. /// @param[in] input Input string in UTF-8 encoding.
  60. /// @param[out] output Output string of UCS-2 characters.
  61. /// @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).
  62. static bool UTF8toUCS2(const String& input, WString& output);
  63. /// Converts an array of words in UCS-2 encoding into a character array in UTF-8 encoding. This
  64. /// function assumes the endianness of the input words to be the same as the host processor.
  65. /// @param[in] input Input string of words in UCS-2 encoding.
  66. /// @param[out] output Output string in UTF-8 encoding.
  67. /// @return True if the conversion went successfully, false if not.
  68. static bool UCS2toUTF8(const WString& input, String& output);
  69. /// Checks if a given value is a whitespace character.
  70. /// @param[in] x The character to evaluate.
  71. /// @return True if the character is whitespace, false otherwise.
  72. template < typename CharacterType >
  73. static bool IsWhitespace(CharacterType x)
  74. {
  75. return (x == '\r' || x == '\n' || x == ' ' || x == '\t');
  76. }
  77. /// Strip whitespace characters from the beginning and end of a string.
  78. /// @param[in] string The string to trim.
  79. /// @return The stripped string.
  80. static String StripWhitespace(const String& string);
  81. /// Operator for STL containers using strings.
  82. struct ROCKETCORE_API StringComparei
  83. {
  84. bool operator()(const String& lhs, const String& rhs) const;
  85. };
  86. };
  87. }
  88. }
  89. #endif