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