PDA

View Full Version : QHash non-ANSI key problem



roxton
27th May 2008, 12:32
Hello! I've found a strange behavior of a hash lookup with a non-ANSI key. When I use Latin keys, hash works fine - all values are returned by the QHash.value function. But when I'm trying to use a Russian keys (in the QString unicode, of course), sometimes hash does not return a value. And vice versa - sometime I can't get a key but the value - even if key or value if a one-character strings.
Also I have to notice that all keys and values are presented in the hash table, as I can check it with keys() and values() functions.
If QHash table have such problems, what you can suggest me to use for key/value handling with a full Unicode support in keys and values?

ChristianEhrlicher
27th May 2008, 13:06
I don't think it's a qhash - problem but something on your side. Plz post some code to show us the problem.

roxton
27th May 2008, 15:26
OK, here is all code that related to my problem. It's a part of my Morse code converter.


//qstring_load - just a utility function for text file loading into QString
QString qstring_load (const QString &fileName)
{
QFile file (fileName);

QString s;

if (! file.open(QFile::ReadOnly | QFile::Text))
return s;

QTextStream in(&file);
in.setCodec ("UTF-8");

s = in.readAll();
return s;
}

//hash_load_keyval - loads a key/value pairs table into the hash:
QHash<QString, QString> hash_load_keyval (const QString &fname)
{
QHash<QString, QString> result;
if (! file_exists (fname))
return result;

QStringList l = qstring_load (fname).split ("\n");

int c = l.size();
for (int i = 0; i < c; i++)
{
QStringList sl = l[i].split("=");
if (sl.size() == 2)
result.insert (sl[0], sl[1]);
}

return result;
}

//morse_from_lang - converts QString s into the Morse code using the key/value file that embeded into the resource. The QString lang is a locale id, for example "en" or "ru".
Please note that all data from the file loads OK.

QString morse_from_lang (const QString &s, const QString &lang)
{

QHash<QString, QString> h = hash_load_keyval (":/text-data/morse-" + lang);

QString result;

int c = s.size();
for (int i = 0; i < c; i++)
{
QString t = h.value (QString (s[i])); //here can be an empty value if key is non-Latin, and I don't know why
if (! t.isNull() || ! t.isEmpty())
result.append (t + " ");
}

return result;
}

ChristianEhrlicher
27th May 2008, 15:38
First: Please use the '[
]' - tags to make your code more readable

I would suggest adding some debug output to see what's going wrong. Maybe the utf-8 codec is not correct or something like this.
Add a debug output to see which keys you really put into the hash. Also use QHash<QChar,QString> because you only test for a char, not a string.
And then put a debug output into your other loop to see what s[i] actually is.

The debug-output should output the unicode numbers:
[code]
QChar c = sl[0][0];
qDebug() << "Adding " << c << " (= " << c.unicode() << ") to the hash";

roxton
27th May 2008, 16:54
Thanks, all works fine now. It was my fault - I've copied a Morse table data from Wikipedia, and there are was some weird stuff with Russian letters (Latin "A" instead on a Russian "A", etc).