RTBKit  0.9
Open-source framework to create real-time ad bidding systems.
soa/types/string.h
00001 /* string.h                                                          -*- C++ -*-
00002    Sunil Rottoo, 27 April 2012
00003    Copyright (c) 20102 Datacratic.  All rights reserved.
00004 
00005    Basic classes for dealing with string including internationalisation
00006 */
00007 
00008 #pragma once
00009 
00010 #include <string>
00011 #include "soa/utf8cpp/source/utf8.h"
00012 #include "jml/db/persistent_fwd.h"
00013 
00014 
00015 namespace Datacratic {
00016 
00017 
00018 /*****************************************************************************/
00019 /* Utf8String                                                               */
00020 /*****************************************************************************/
00021 
00022 namespace JS {
00023 struct JSValue;
00024 } // namespace JS
00025 
00026 
00027 class Utf8String
00028 {
00029 public:
00030     static Utf8String fromLatin1(const std::string & lat1Str);
00031 
00033     Utf8String()
00034     {
00035     }
00036 
00038     Utf8String(Utf8String && str) noexcept
00039         : data_(std::move(str.data_))
00040     {
00041     }
00042 
00044     Utf8String(const Utf8String & str)
00045         : data_(str.data_)
00046     {
00047     }
00048 
00055     explicit Utf8String(const std::string &in, bool check=true) ;
00056 
00057     explicit Utf8String(std::string &&in, bool check=true) ;
00058 
00059     Utf8String & operator=(Utf8String && str) noexcept
00060     {
00061         Utf8String newMe(std::move(str));
00062         swap(newMe);
00063         return *this;
00064     }
00065 
00066     Utf8String & operator=(const Utf8String & str)
00067     {
00068         Utf8String newMe(str);
00069         swap(newMe);
00070         return *this;
00071     }
00072 
00073     Utf8String & operator=(const std::string &str)
00074     {
00075         data_ = str;
00076         return *this;
00077     }
00078 
00079     Utf8String & operator=(std::string &&str)
00080     {
00081         data_ = std::move(str);
00082         return *this;
00083     }
00084 
00085     void swap(Utf8String & other)
00086     {
00087         data_.swap(other.data_);
00088     }
00089 
00090     bool empty() const
00091     {
00092         return data_.empty();
00093     }
00094 
00095     typedef utf8::iterator<std::string::const_iterator> const_iterator;
00096     typedef utf8::iterator<std::string::iterator> iterator;
00097 
00098     const_iterator begin() const;
00099     const_iterator end() const ;
00100 
00101     Utf8String&  operator+=(const std::string& str)
00102     {
00103         data_+=str;
00104         return *this;
00105     }
00106     Utf8String &operator+=(const Utf8String &utf8str);
00107     /*
00108      * Returns access to the underlying representation - unsafe
00109      */
00110     const std::string & rawString() const { return data_; }
00111     const char * rawData() const { return data_.c_str(); }
00112     size_t rawLength() const { return data_.length() ; }
00113 
00114     void serialize(ML::DB::Store_Writer & store) const;
00115     void reconstitute(ML::DB::Store_Reader & store);
00116 
00117     std::string extractAscii();
00118 
00119     bool operator == (const Utf8String & other) const
00120     {
00121         return data_ == other.data_;
00122     }
00123 
00124     bool operator != (const Utf8String & other) const
00125     {
00126         return data_ != other.data_;
00127     }
00128 
00129     bool operator < (const Utf8String & other) const
00130     {
00131         return data_ < other.data_;
00132     }
00133 
00134 private:
00135     std::string data_; // original utf8-encoded string
00136 };
00137 
00138 inline void swap(Utf8String & s1, Utf8String & s2)
00139 {
00140     s1.swap(s2);
00141 }
00142 
00143 std::ostream & operator << (std::ostream & stream, const Utf8String & str);
00144 
00145 IMPL_SERIALIZE_RECONSTITUTE(Utf8String);
00146 
00147 #if 0
00148 namespace JS {
00149 
00150 void to_js(JSValue & jsval, const Utf8String & value);
00151 Utf8String from_js(const JSValue & val, Utf8String *);
00152 
00153 } // namespace JS
00154 #endif
00155 } // namespace Datacratic
00156 
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator