2011-11-16 02:10:29 +00:00
|
|
|
|
|
|
|
// Copyright Ferdinand Majerech 2011.
|
|
|
|
// Distributed under the Boost Software License, Version 1.0.
|
|
|
|
// (See accompanying file LICENSE_1_0.txt or copy at
|
|
|
|
// http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
|
|
|
|
///Zero terminated string.
|
|
|
|
module dyaml.zerostring;
|
|
|
|
|
|
|
|
import core.stdc.string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Zero terminated string used to decrease data structure size.
|
|
|
|
*
|
|
|
|
* TypeName is used to differentiate types (better than simple alias).
|
|
|
|
*/
|
|
|
|
struct ZeroString(string TypeName)
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
///Zero terminated string.
|
|
|
|
immutable(char)* str_ = null;
|
|
|
|
|
|
|
|
public:
|
|
|
|
@disable int opCmp(ref ZeroString);
|
|
|
|
|
|
|
|
///Construct a string.
|
2012-09-13 19:37:28 +00:00
|
|
|
this(const string str) pure nothrow @safe
|
2011-11-16 02:10:29 +00:00
|
|
|
{
|
|
|
|
if(str is null || str == "")
|
|
|
|
{
|
|
|
|
str_ = null;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
str_ = (str ~ '\0').ptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
///Get the string.
|
2012-08-30 13:38:51 +00:00
|
|
|
@property string get() const nothrow @trusted
|
2011-11-16 02:10:29 +00:00
|
|
|
in{assert(!isNull());}
|
|
|
|
body
|
|
|
|
{
|
|
|
|
return cast(string)str_[0 .. strlen(str_)];
|
|
|
|
}
|
|
|
|
|
|
|
|
///Test for equality with another string.
|
2012-12-27 21:04:42 +00:00
|
|
|
bool opEquals(const ZeroString str) const nothrow @trusted
|
2011-11-16 02:10:29 +00:00
|
|
|
{
|
|
|
|
return isNull ? str.isNull :
|
|
|
|
str.isNull ? false : (0 == strcmp(str_, str.str_));
|
|
|
|
}
|
|
|
|
|
|
|
|
///Compute a hash.
|
2012-08-30 13:38:51 +00:00
|
|
|
hash_t toHash() const nothrow @safe
|
2011-11-16 02:10:29 +00:00
|
|
|
in{assert(!isNull);}
|
|
|
|
body
|
|
|
|
{
|
|
|
|
auto str = get();
|
2012-08-30 13:38:51 +00:00
|
|
|
return getHash(str);
|
2011-11-16 02:10:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///Compare with another string.
|
2012-09-08 23:42:13 +00:00
|
|
|
int opCmp(const ref ZeroString str) const nothrow @trusted
|
2011-11-16 02:10:29 +00:00
|
|
|
in{assert(!isNull && !str.isNull);}
|
|
|
|
body
|
|
|
|
{
|
|
|
|
return strcmp(str_, str.str_);
|
|
|
|
}
|
|
|
|
|
|
|
|
///Is this string null (invalid)?
|
2012-09-08 23:42:13 +00:00
|
|
|
@property bool isNull() pure const nothrow @safe {return str_ is null;}
|
2012-08-30 13:38:51 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
///Hack to allow toHash to be @safe.
|
|
|
|
//
|
|
|
|
//To remove this hack, need a typeid(string).getHash() replacement that does not take a pointer.
|
|
|
|
hash_t getHash(ref string str) const nothrow @trusted
|
|
|
|
{
|
|
|
|
return typeid(string).getHash(&str);
|
|
|
|
}
|
2011-11-16 02:10:29 +00:00
|
|
|
}
|