Files
externals/linux/include/OnixS/CME/MDP3/String.h
T
2017-08-09 14:33:06 +00:00

349 lines
9.6 KiB
C++
Executable File

// Copyright Onix Solutions Limited [OnixS]. All rights reserved.
//
// This software owned by Onix Solutions Limited [OnixS] and is protected by copyright law
// and international copyright treaties.
//
// Access to and use of the software is governed by the terms of the applicable OnixS Software
// Services Agreement (the Agreement) and Customer end user license agreements granting
// a non-assignable, non-transferable and non-exclusive license to use the software
// for it's own data processing purposes under the terms defined in the Agreement.
//
// Except as otherwise granted within the terms of the Agreement, copying or reproduction of any part
// of this source code or associated reference material to any other location for further reproduction
// or redistribution, and any amendments to this copyright notice, are expressly prohibited.
//
// Any reproduction or redistribution for sale or hiring of the Software not in accordance with
// the terms of the Agreement is a violation of copyright law.
//
#pragma once
#include <cstring>
#include <string>
#include <stdexcept>
#include <functional>
#include <ostream>
#include "OnixS/CME/MDP3/Config.h"
#include "OnixS/CME/MDP3/Numeric.h"
namespace OnixS
{
namespace CME
{
namespace MDP3
{
/// Provides efficient way of accessing text-based FIX field values.
class
ONIXS_CME_MDP3_API
StringRef
{
public:
/// Immutable iterator over chars.
typedef const char* ConstIterator;
/// Initializes blank instance.
StringRef()
: chars_(NULL),
size_(0)
{
}
/// Full initialization.
StringRef(
const char* chars,
size_t size)
: chars_(chars),
size_(size)
{
}
/// Initializes as clone of other instance.
/// Data referenced by clone is not copied.
/// Instead both instances will refer to same
/// text segment.
StringRef(
const StringRef& other)
: chars_(other.chars_),
size_(other.size_)
{
}
/// Initializes instance from string content.
explicit StringRef(
const std::string& stdStr)
: chars_(stdStr.c_str()),
size_(stdStr.size())
{
}
/// Initializes instance from zero-terminated string.
explicit StringRef(const char* cStr)
: chars_(cStr), size_(cStr ? strlen(cStr) : 0)
{
}
/// Indicates whether array of zero length.
bool empty() const
{
return (0 == size_);
}
/// Read-only content.
const char* data() const
{
return chars_;
}
/// STL-like begin().
ConstIterator begin() const
{
return chars_;
}
/// STL-like end().
ConstIterator end() const
{
return chars_ + size_;
}
/// Number of chars.
size_t size() const
{
return size_;
}
/// Resets reference to nothing.
void reset()
{
reset(NULL, 0);
}
/// Updates data being referenced.
void
reset(
const char* chars,
size_t size)
{
chars_ = chars;
size_ = size;
}
const char&
operator [](size_t index) const
{
return chars_[index];
}
const char&
at(size_t index) const
{
if (index < size_)
return chars_[index];
throw std::invalid_argument("index");
}
/// Returns number if text is string
/// representation of an integer.
template
<
typename NumericType
>
bool
toNumber(NumericType& number) const
{
return OnixS::CME::MDP3::Number::
tryParse(chars_, size_, number);
}
// Appends copy of text to the std::string.
void toString(std::string& str) const
{
str.append(chars_, size_);
}
// Return copy of text as std::string.
std::string toString() const
{
return std::string(chars_, size_);
}
/// Implicit conversion to std::string.
operator std::string() const
{
return std::string(chars_, size_);
}
/// Compares with another instance.
bool
operator ==(
const StringRef& other) const
{
return (
size_ == other.size_
&&
0 == memcmp(
chars_, other.chars_, size_) );
}
/// Compares with another instance.
bool
operator !=(
const StringRef& other) const
{
return (
size_ != other.size_
||
0 != memcmp(
chars_, other.chars_, size_) );
}
/// Reinitializes from another instance.
StringRef&
operator =(
const StringRef& other)
{
chars_ = other.chars_;
size_ = other.size_;
return *this;
}
/// Swaps content with other instance.
void
swap(StringRef& other)
{
std::swap(chars_, other.chars_);
std::swap(size_, other.size_);
}
private:
/// Items being referenced.
const char* chars_;
/// Number of chars being referenced.
size_t size_;
};
inline
bool
operator ==(
const StringRef& ref,
const std::string& str)
{
return ref == StringRef(str);
}
inline
bool
operator !=(
const StringRef& ref,
const std::string& str)
{
return ref != StringRef(str);
}
inline
bool
operator ==(
const std::string& str,
const StringRef& ref)
{
return ref == StringRef(str);
}
inline
bool
operator !=(
const std::string& str,
const StringRef& ref)
{
return ref != StringRef(str);
}
//
inline
bool
operator ==(
const StringRef& ref,
const char* str)
{
return ref == StringRef(str);
}
inline
bool
operator !=(
const StringRef& ref,
const char* str)
{
return ref != StringRef(str);
}
inline
bool
operator ==(
const char* str,
const StringRef& ref)
{
return ref == StringRef(str);
}
inline
bool
operator !=(
const char* str,
const StringRef& ref)
{
return ref != StringRef(str);
}
}
}
}
namespace std
{
/// Allows using of StringRef in collections like std::map.
template<>
struct
ONIXS_CME_MDP3_API
less<OnixS::CME::MDP3::StringRef>
{
bool
operator ()(
const OnixS::CME::MDP3::StringRef& left,
const OnixS::CME::MDP3::StringRef& right) const
{
ptrdiff_t
compareResult = memcmp(
left.data(), right.data(),
left.size() < right.size()
? left.size() : right.size());
if (0 == compareResult)
{
compareResult =
static_cast<ptrdiff_t> (
left.size() - right.size() );
}
return (0 > compareResult);
}
};
inline
std::ostream&
operator <<(
std::ostream& stream,
const OnixS::CME::MDP3::StringRef& text)
{
stream.write(text.data(), text.size());
return stream;
}
}