Drizzled Public API Documentation

string.h
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  * Copyright (C) 2010 Brian Aker
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *   * Redistributions of source code must retain the above copyright notice,
00011  *     this list of conditions and the following disclaimer.
00012  *   * Redistributions in binary form must reproduce the above copyright notice,
00013  *     this list of conditions and the following disclaimer in the documentation
00014  *     and/or other materials provided with the distribution.
00015  *   * Neither the name of Patrick Galbraith nor the names of its contributors
00016  *     may be used to endorse or promote products derived from this software
00017  *     without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00029  * THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 /*
00033   Some sections of this code came from the Boost examples.
00034 */
00035 
00036 #pragma once
00037 
00038 #include <utility>
00039 #include <string>
00040 #include <vector>
00041 
00042 #include <boost/algorithm/string/predicate.hpp>
00043 #include <boost/foreach.hpp>
00044 #include <boost/functional/hash.hpp>
00045 #include <boost/shared_ptr.hpp>
00046 
00047 #define drizzle_literal_parameter(X) (X), size_t((sizeof(X) - 1))
00048 
00049 namespace drizzled {
00050 namespace util {
00051 
00052 struct insensitive_equal_to : std::binary_function<std::string, std::string, bool>
00053 {
00054   bool operator()(std::string const& x, std::string const& y) const
00055   {
00056     return boost::algorithm::iequals(x, y);
00057   }
00058 };
00059 
00060 struct insensitive_hash : std::unary_function<std::string, std::size_t>
00061 {
00062   std::size_t operator()(std::string const& x) const
00063   {
00064     std::size_t seed = 0;
00065     BOOST_FOREACH(std::string::const_reference it, x)
00066       boost::hash_combine(seed, std::toupper(it));
00067     return seed;
00068   }
00069 };
00070 
00071 struct sensitive_hash : std::unary_function< std::vector<char>, std::size_t>
00072 {
00073   std::size_t operator()(std::vector<char> const& x) const
00074   {
00075     std::size_t seed = 0;
00076     BOOST_FOREACH(std::vector<char>::const_reference it, x)
00077       boost::hash_combine(seed, it);
00078     return seed;
00079   }
00080 };
00081 
00082 class String
00083 {
00084 public:
00085 
00086   String()
00087   {
00088     _bytes.resize(1);
00089   }
00090 
00091   const char* data() const
00092   {
00093     return &_bytes[0];
00094   }
00095 
00096   char* data()
00097   {
00098     return &_bytes[0];
00099   }
00100 
00101   void assign(const size_t repeat, const char arg)
00102   {
00103     _bytes.resize(repeat + 1); // inserts \0 at end
00104     memset(&_bytes[0], arg, repeat);
00105     _bytes[repeat]= 0;
00106   }
00107 
00108   void assign(const char *arg, const size_t arg_size)
00109   {
00110     _bytes.resize(arg_size + 1); // +1 for \0
00111     memcpy(&_bytes[0], arg, arg_size);
00112     _bytes[arg_size]= 0;
00113   }
00114 
00115   void append(const char *arg, const size_t arg_size)
00116   {
00117     if (not arg or not arg_size)
00118       return;
00119 
00120     size_t original_size= size();
00121     if (original_size)
00122     {
00123       _bytes.resize(original_size + arg_size + 1); // inserts NULL since string will already have a NULL
00124       memcpy(&_bytes[original_size], arg, arg_size);
00125       _bytes[original_size + arg_size]= 0;
00126     }
00127     else
00128     {
00129       assign(arg, arg_size);
00130     }
00131   }
00132 
00133   const char& operator[] (size_t arg) const
00134   {
00135     return _bytes[arg];
00136   }
00137 
00138   char& operator[] (size_t arg)
00139   {
00140     return _bytes[arg];
00141   }
00142 
00143   void clear()
00144   {
00145     _bytes.resize(1);
00146     _bytes[0]= 0;
00147   }
00148 
00149   size_t size() const
00150   {
00151     return _bytes.size() -1;
00152   }
00153 
00154 private:
00155   std::vector<char> _bytes;
00156 };
00157 
00158 } /* namespace util */
00159 } /* namespace drizzled */
00160