[prev in list] [next in list] [prev in thread] [next in thread] 

List:       kde-core-devel
Subject:    [PATCH][RFC] KCharsets::codecForName() cleanups
From:       Marc Mutz <Marc () Mutz ! com>
Date:       2001-11-07 22:26:22
[Download RAW message or body]

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi!

I'd like to propose some bic changes to KCharsets. The reason for this 
is that some of it's functions take QString's where QCStrings of char* 
would be appropriate.

E.g. ::codecForName(QString,bool). All it does is to immediately call 
.latin1() on the QString and never again uses the unicode data.
Since charset names cannot contain non-US-ASCII characters, we should 
use char* (more appropriate, since codecForNameDict is a QAsciiDict 
anyway), with a wrapper for QCStrings (maybe). If someone has a QString 
source of charset names, he could always call codecForName( 
mycharset.latin1() ) directly.

The changes in detail:

- - - made (QString) a wrapper around (const char*,bool&).
- - - made (QCString,bool&) a wrapper around ibid.
- - - made !str.isEmpty() checks str && *str (this swaps if and else 
branches)
- - - removed superfluous .isNull() checking for cname
- - - used KLocale::encoding() instead of deprecated KLocale::charset().

Some ideas and questions regarding charsets.config:

- - - Why not use key->MIB number mappings in the charsets file (instead of 
key->normalized charset name)? Should be faster.

- - - Why are there entries in the builtin section that only change space to 
dashes and vice versa. AFAIK, Qt's codecForName() already normalizes 
the names by (among other things) removing any whitespace and dashes.

Marc

- - --
It is truly ironic that the United States, once the beacon for
promoting the principles of freedom of expression, is now
systematically infecting other countries with this dangerous public
policy choice [the DMCA] that will restrict more speech than any law
before it.    -- EFF FTAA Alert:
                 Stop Hollywood Forcing Technology Ban on 34 Countries
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE76bUP3oWD+L2/6DgRAjOwAJ9I65WqTV2ItYI4++Bi85IAnATTAQCggqPU
wAsPjSvnk1kG1RG/182dzNM=
=ExRH
-----END PGP SIGNATURE-----

["kcharset-codec-for-name-cleanup.diff;" (text/x-diff)]

Index: kcharsets.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kcharsets.cpp,v
retrieving revision 1.118
diff -u -3 -p -r1.118 kcharsets.cpp
--- kcharsets.cpp	2001/10/29 17:26:47	1.118
+++ kcharsets.cpp	2001/11/07 17:22:17
@@ -209,29 +209,34 @@ QStringList KCharsets::descriptiveEncodi
 QTextCodec *KCharsets::codecForName(const QString &n) const
 {
     bool b;
-    return codecForName( n, b );
+    return codecForName( n.latin1(), b );
 }
 
-QTextCodec *KCharsets::codecForName(const QString &n, bool &ok) const
+QTextCodec *KCharsets::codecForName(const QCString &n, bool &ok) const
 {
+  return codecForName( n.data(), ok );
+}
+
+QTextCodec *KCharsets::codecForName(const char * n, bool &ok) const
+{
     ok = true;
 
     QTextCodec* codec = 0;
     // dict lookup is case insensitive anyway
-    if((codec = d->codecForNameDict[n.isEmpty() ? "->locale<-" : n.latin1()]))
+    if((codec = d->codecForNameDict[ ( n && *n ) ? n : "->locale<-" ]))
         return codec; // cache hit, return
 
-    QCString name = n.lower().latin1();
+    QCString name = QCString(n).lower();
     QCString key = name;
     if (name.right(8) == "_charset")
        name.truncate(name.length()-8);
 
-    if (n.isEmpty()) {
-        QString lc = KGlobal::locale()->charset();
-        if (lc.isEmpty())
-            codec = QTextCodec::codecForName("iso8859-1");
-        else
+    if ( key.isEmpty() ) {
+        const char *lc = KGlobal::locale()->encoding();
+        if (lc && *lc)
             codec = codecForName(lc);
+        else
+            codec = QTextCodec::codecForName("iso8859-1");
 
         d->codecForNameDict.replace("->locale<-", codec);
         return codec;
@@ -249,7 +254,7 @@ QTextCodec *KCharsets::codecForName(cons
     d->conf->setGroup("builtin");
 
     QString cname = d->conf->readEntry(name.data());
-    if(!cname.isEmpty() && !cname.isNull())
+    if(!cname.isEmpty())
         codec = QTextCodec::codecForName(cname.latin1());
 
     if(codec)
@@ -267,7 +272,7 @@ QTextCodec *KCharsets::codecForName(cons
     d->conf->setGroup("aliases");
 
     cname = d->conf->readEntry(name.data());
-    if(cname.isNull() || cname.isEmpty())
+    if(cname.isEmpty())
         cname = name;
     cname = cname.upper();
 
@@ -284,7 +289,7 @@ QTextCodec *KCharsets::codecForName(cons
     cname = cname.lower();
     cname = d->conf->readEntry(cname);
 
-    if(!cname.isEmpty() && !cname.isNull())
+    if(!cname.isEmpty())
         codec = QTextCodec::codecForName(cname.latin1());
 
     if(codec) {
Index: kcharsets.h
===================================================================
RCS file: /home/kde/kdelibs/kdecore/kcharsets.h,v
retrieving revision 1.48
diff -u -3 -p -r1.48 kcharsets.h
--- kcharsets.h	2001/11/02 22:27:27	1.48
+++ kcharsets.h	2001/11/07 17:22:17
@@ -57,7 +57,7 @@ public:
     virtual ~KCharsets();
 
     /**
-     * provided for compitibility.
+     * provided for compatibility.
      */
     QTextCodec *codecForName(const QString &name) const;
     
@@ -66,7 +66,12 @@ public:
      * Unicode. If no codec could be found the latin1 codec will be returned an
      * ok will be set to false.
      */
-    QTextCodec *codecForName(const QString &n, bool &ok) const;
+    QTextCodec *codecForName(const char *n, bool &ok) const;
+
+    /**
+     * provided for convenience.
+     */
+    QTextCodec *codecForName(const QCString &n, bool &ok) const;
 
     /**
      * converts an entity to a character. The string must contain only the
Index: klocale.cpp
===================================================================
RCS file: /home/kde/kdelibs/kdecore/klocale.cpp,v
retrieving revision 1.286
diff -u -3 -p -r1.286 klocale.cpp
--- klocale.cpp	2001/11/05 22:44:21	1.286
+++ klocale.cpp	2001/11/07 17:22:19
@@ -1575,7 +1575,7 @@ void KLocale::initCharset(KConfig *confi
     {
       // ### HPB: This code should be rewritten/removed
       bool bOk;
-      KGlobal::charsets()->codecForName(m_charset, bOk);
+      KGlobal::charsets()->codecForName(m_charset.latin1(), bOk);
       // if !ok, we have a problem. it will return latin-1 then, but thats
       // obviously not what the user wants
       if(!bOk)


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic