PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/mordor/string.h

http://github.com/mozy/mordor
C Header | 128 lines | 94 code | 22 blank | 12 comment | 4 complexity | 724cb7a646d40bb075d30f42f533e160 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_STRING_H__
  2. #define __MORDOR_STRING_H__
  3. // Copyright (c) 2009 Mozy, Inc.
  4. #include <ostream>
  5. #include <string>
  6. #include <vector>
  7. #ifndef BOOST_TYPEOF_SILENT
  8. #define BOOST_TYPEOF_SILENT
  9. #endif
  10. #include <boost/typeof/typeof.hpp>
  11. #include "version.h"
  12. #ifdef OSX
  13. #include <CoreFoundation/CFString.h>
  14. #endif
  15. namespace Mordor {
  16. std::string base64decode(const std::string &src);
  17. std::string base64encode(const std::string &data);
  18. std::string base64encode(const void *data, size_t len);
  19. // Returns result in hex
  20. std::string md5(const std::string &data);
  21. std::string sha1(const std::string &data);
  22. // Returns result in blob
  23. std::string md5sum(const std::string &data);
  24. std::string md5sum(const void *data, size_t len);
  25. std::string sha0sum(const std::string &data);
  26. std::string sha0sum(const void *data, size_t len);
  27. std::string sha1sum(const std::string &data);
  28. std::string sha1sum(const void *data, size_t len);
  29. std::string hmacMd5(const std::string &text, const std::string &key);
  30. std::string hmacSha1(const std::string &text, const std::string &key);
  31. std::string hmacSha256(const std::string &text, const std::string &key);
  32. /// Output must be of size len * 2, and will *not* be null-terminated
  33. void hexstringFromData(const void *data, size_t len, char *output);
  34. std::string hexstringFromData(const void *data, size_t len);
  35. std::string hexstringFromData(const std::string &data);
  36. /// Output must be of size length / 2, and will *not* be null-terminated
  37. /// std::invalid_argument will be thrown if hexstring is not hex
  38. void dataFromHexstring(const char *hexstring, size_t length, void *output);
  39. std::string dataFromHexstring(const char *hexstring, size_t length);
  40. std::string dataFromHexstring(const std::string &data);
  41. void replace(std::string &str, char find, char replaceWith);
  42. void replace(std::string &str, char find, const std::string &replaceWith);
  43. void replace(std::string &str, const std::string &find, const std::string &replaceWith);
  44. std::vector<std::string> split(const std::string &str, char delim, size_t max = ~0);
  45. std::vector<std::string> split(const std::string &str, const char *delims, size_t max = ~0);
  46. /// Converts a human readable string to microseconds
  47. ///
  48. /// String must be of the format "<value> [<units>]".
  49. /// Value can be integer or floating point.
  50. /// Units can be microseconds, milliseconds, seconds, minutes, hours, or days,
  51. /// or the abbreviation of it (us, ms, s, m, h, d), and if omitted implies us.
  52. unsigned long long stringToMicroseconds(const std::string &string);
  53. namespace detail
  54. {
  55. template <class T>
  56. typename boost::enable_if_c<sizeof(T) == 2, wchar_t>::type utf16func();
  57. template <class T>
  58. typename boost::disable_if_c<sizeof(T) == 2, unsigned short>::type utf16func();
  59. template <class T>
  60. typename boost::enable_if_c<sizeof(T) == 4, wchar_t>::type utf32func();
  61. template <class T>
  62. typename boost::disable_if_c<sizeof(T) == 4, unsigned int>::type utf32func();
  63. };
  64. typedef BOOST_TYPEOF(detail::utf16func<wchar_t>()) utf16char;
  65. typedef BOOST_TYPEOF(detail::utf32func<wchar_t>()) utf32char;
  66. typedef std::basic_string<utf16char> utf16string;
  67. typedef std::basic_string<utf32char> utf32string;
  68. #ifdef WINDOWS
  69. std::string toUtf8(const utf16char *str, size_t len);
  70. std::string toUtf8(const utf16string &str);
  71. utf16string toUtf16(const char *str, size_t len);
  72. utf16string toUtf16(const std::string &str);
  73. #elif defined (OSX)
  74. std::string toUtf8(CFStringRef string);
  75. utf16string toUtf16(const char *str, size_t len);
  76. utf16string toUtf16(const std::string &str);
  77. #elif defined (LINUX)
  78. utf16string toUtf16(const char *str, size_t len);
  79. utf16string toUtf16(const std::string &str);
  80. #endif
  81. std::string toUtf8(utf16char character);
  82. std::string toUtf8(utf32char character);
  83. utf32char toUtf32(utf16char highSurrogate, utf16char lowSurrogate);
  84. std::string toUtf8(utf16char highSurrogate, utf16char lowSurrogate);
  85. bool isHighSurrogate(utf16char character);
  86. bool isLowSurrogate(utf16char character);
  87. bool validateUtf8(const std::string &str);
  88. struct caseinsensitiveless
  89. {
  90. bool operator()(const std::string& lhs, const std::string& rhs) const;
  91. };
  92. struct charslice
  93. {
  94. friend std::ostream &operator <<(std::ostream &os, const charslice &slice);
  95. public:
  96. charslice(const char *slice, size_t len)
  97. : m_slice(slice),
  98. m_len(len)
  99. {}
  100. private:
  101. const char *m_slice;
  102. size_t m_len;
  103. };
  104. std::ostream &operator <<(std::ostream &os, const charslice &slice);
  105. }
  106. #endif