1 /***************************************************************************
2 Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>
3 ***************************************************************************/
5 /***************************************************************************
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
12 ***************************************************************************/
14 #ifndef INDEXED_STRING_H
15 #define INDEXED_STRING_H
17 //krazy:excludeall=dpointer,inline
19 #include <QtCore/QString>
20 #include "../languageexport.h"
31 //Empty strings have an index of zero.
32 //Strings of length one are not put into the repository, but are encoded directly within the index:
33 //They are encoded like 0xffff00bb where bb is the byte of the character.
34 class KDEVPLATFORMLANGUAGE_EXPORT IndexedString
{
37 ///@param str must be a utf8 encoded string, does not need to be 0-terminated.
38 ///@param length must be its length in bytes.
39 ///@param hash must be a hash as constructed with the here defined hash functions. If it is zero, it will be computed.
40 explicit IndexedString( const char* str
, unsigned short length
, unsigned int hash
= 0 );
42 ///Needs a zero terminated string. When the information is already available, try using the other constructor.
43 explicit IndexedString( const char* str
);
45 explicit IndexedString( char c
);
47 ///When the information is already available, try using the other constructor. This is expensive.
48 explicit IndexedString( const QString
& str
);
50 ///When the information is already available, try using the other constructor. This is expensive.
51 explicit IndexedString( const QByteArray
& str
);
53 explicit IndexedString( unsigned int index
) : m_index(index
) {
56 ///Creates an indexed string from a KUrl, this is expensive.
57 explicit IndexedString( const KUrl
& url
);
59 ///Re-construct a KUrl from this indexed string, the result can be used with the
60 ///KUrl-using constructor. This is expensive.
63 inline unsigned int hash() const {
67 //The string is uniquely identified by this index. You can use it for comparison.
68 inline unsigned int index() const {
72 bool isEmpty() const {
76 //This is relatively expensive(needs a mutex lock, hash lookups, and eventual loading), so avoid it when possible.
79 ///Convenience function, avoid using it, it's relatively expensive
82 ///Convenience function, avoid using it, it's relatively expensive(les expensive then str() though)
83 QByteArray
byteArray() const;
85 bool operator == ( const IndexedString
& rhs
) const {
86 return m_index
== rhs
.m_index
;
89 bool operator != ( const IndexedString
& rhs
) const {
90 return m_index
!= rhs
.m_index
;
93 ///Does not compare alphabetically, uses the index for ordering.
94 bool operator < ( const IndexedString
& rhs
) const {
95 return m_index
< rhs
.m_index
;
98 //Use this to construct a hash-value on-the-fly
99 //To read it, just use the hash member, and when a new string is started, call clear(.
100 //This needs very fast performance(per character operation), so it must stay inlined.
103 HashInitialValue
= 5381
106 RunningHash() : hash(HashInitialValue
) { //We initialize the hash with zero, because we want empty strings to create a zero hash(invalid)
108 inline void append(const char c
) {
109 hash
= ((hash
<< 5) + hash
) + c
; /* hash * 33 + c */
111 inline void clear() {
112 hash
= HashInitialValue
;
117 static unsigned int hashString(const char* str
, unsigned short length
);
123 KDEVPLATFORMLANGUAGE_EXPORT
inline uint
qHash( const KDevelop::IndexedString
& str
) {