diff options
author | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:17:32 +0000 |
---|---|---|
committer | tpearson <tpearson@283d02a7-25f6-0310-bc7c-ecb5cbfe19da> | 2010-03-01 19:17:32 +0000 |
commit | e38d2351b83fa65c66ccde443777647ef5cb6cff (patch) | |
tree | 1897fc20e9f73a81c520a5b9f76f8ed042124883 /src/latin1literal.h | |
download | tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.tar.gz tellico-e38d2351b83fa65c66ccde443777647ef5cb6cff.zip |
Added KDE3 version of Tellico
git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/applications/tellico@1097620 283d02a7-25f6-0310-bc7c-ecb5cbfe19da
Diffstat (limited to 'src/latin1literal.h')
-rw-r--r-- | src/latin1literal.h | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/latin1literal.h b/src/latin1literal.h new file mode 100644 index 0000000..03e2293 --- /dev/null +++ b/src/latin1literal.h @@ -0,0 +1,90 @@ +/*************************************************************************** + copyright : (C) 2003-2006 by Robby Stephenson + email : [email protected] + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of version 2 of the GNU General Public License as * + * published by the Free Software Foundation; * + * * + ***************************************************************************/ + +// this code was original published to the kde-core-devel email list +// copyright 2003 Harri Porten <[email protected]> +// Originally licensed under LGPL, included here under GPL v2 + +#ifndef LATIN1LITERAL_H +#define LATIN1LITERAL_H + +#include <qstring.h> + +namespace Tellico { + +/** + * A class for explicit marking of string literals encoded in the ISO + * 8859-1 character set. Allows for efficient, still (in terms of the + * chosen encoding) safe comparison with QString instances. To be used + * like this: + * + * \code + * QString s = ..... + * if (s == Latin1Literal("o")) { ..... } + * \endcode + * + */ +#define Latin1Literal(s) \ + Tellico::Latin1LiteralInternal((s), sizeof(s)/sizeof(char)-1) + +class Latin1LiteralInternal { + +public: + Latin1LiteralInternal(const char* s, size_t l) + : str(s), len(s ? l : (size_t)-1) { } + + // this is lazy, leave these public since I can't figure out + // how to declare a friend function that works for gcc 2.95 + const char* str; + size_t len; +}; + +} // end namespace + +inline +bool operator==(const QString& s1, const Tellico::Latin1LiteralInternal& s2) { + const QChar* uc = s1.unicode(); + const char* c = s2.str; + if(!c || !uc) { + return (!c && !uc); + } + + const size_t& l = s2.len; + if(s1.length() != l) { + return false; + } + + for(size_t i = 0; i < l; ++i, ++uc, ++c) { + if(uc->unicode() != static_cast<uchar>(*c)) { + return false; + } + } + return true; +} + +inline +bool operator!=(const QString& s1, const Tellico::Latin1LiteralInternal& s2) { + return !(s1 == s2); +} + +inline +bool operator==(const Tellico::Latin1LiteralInternal& s1, const QString& s2) { + return s2 == s1; +} + +inline +bool operator!=(const Tellico::Latin1LiteralInternal& s1, const QString& s2) { + return !(s2 == s1); +} + +#endif |