Author: pepeto
Date: Thu Dec 18 00:56:38 2014
New Revision: 27338

URL: http://svn.gna.org/viewcvs/freeciv?rev=27338&view=rev
Log:
Replace switch statement by an array of strings in packet_name().

See gna patch #5567

Modified:
    trunk/common/generate_packets.py

Modified: trunk/common/generate_packets.py
URL: 
http://svn.gna.org/viewcvs/freeciv/trunk/common/generate_packets.py?rev=27338&r1=27337&r2=27338&view=diff
==============================================================================
--- trunk/common/generate_packets.py    (original)
+++ trunk/common/generate_packets.py    Thu Dec 18 00:56:38 2014
@@ -1340,15 +1340,26 @@
 def get_packet_name(packets):
     intro='''const char *packet_name(enum packet_type type)
 {
-  switch (type) {
-
-'''
+  static const char *const names[PACKET_LAST] = {
+'''
+
+    mapping={}
+    for p in packets:
+        mapping[p.type_number]=p
+    sorted=list(mapping.keys())
+    sorted.sort()
+
+    last=-1
     body=""
-    for p in packets:
-        body=body+'  case %(type)s:\n    return "%(type)s";\n\n'%p.__dict__
-    extro='''  default:
-    return "unknown";
-  }
+    for n in sorted:
+        for i in range(last + 1, n):
+            body=body+'    "unknown",\n'
+        body=body+'    "%s",\n'%mapping[n].type
+        last=n
+
+    extro='''  };
+
+  return (type >= 0 && type < PACKET_LAST ? names[type] : "unknown");
 }
 
 '''


_______________________________________________
Freeciv-commits mailing list
Freeciv-commits@gna.org
https://mail.gna.org/listinfo/freeciv-commits

Reply via email to