summaryrefslogtreecommitdiff
path: root/error.c
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-07-18 08:45:29 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2012-07-18 08:45:29 +0000
commit6e018e3f492c791b725c9ee9d0b5fc023c88cd52 (patch)
treeca09d329e83506cbcf869ed45a7593b275fbb4b1 /error.c
parent40ce1eb4035de0707549bcf2c7210b2ef11359ef (diff)
error.c: map by index
* error.c (rb_builtin_type_name): map by index. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@36441 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'error.c')
-rw-r--r--error.c71
1 files changed, 37 insertions, 34 deletions
diff --git a/error.c b/error.c
index 2d7c07a49d..9b93e2694f 100644
--- a/error.c
+++ b/error.c
@@ -25,6 +25,8 @@
#include <unistd.h>
#endif
+#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
+
#ifndef EXIT_SUCCESS
#define EXIT_SUCCESS 0
#endif
@@ -387,45 +389,46 @@ rb_compile_bug(const char *file, int line, const char *fmt, ...)
abort();
}
-static const struct types {
- int type;
- const char *name;
-} builtin_types[] = {
- {T_NIL, "nil"},
- {T_OBJECT, "Object"},
- {T_CLASS, "Class"},
- {T_ICLASS, "iClass"}, /* internal use: mixed-in module holder */
- {T_MODULE, "Module"},
- {T_FLOAT, "Float"},
- {T_STRING, "String"},
- {T_REGEXP, "Regexp"},
- {T_ARRAY, "Array"},
- {T_FIXNUM, "Fixnum"},
- {T_HASH, "Hash"},
- {T_STRUCT, "Struct"},
- {T_BIGNUM, "Bignum"},
- {T_FILE, "File"},
- {T_RATIONAL,"Rational"},
- {T_COMPLEX, "Complex"},
- {T_TRUE, "true"},
- {T_FALSE, "false"},
- {T_SYMBOL, "Symbol"}, /* :symbol */
- {T_DATA, "Data"}, /* internal use: wrapped C pointers */
- {T_MATCH, "MatchData"}, /* data of $~ */
- {T_NODE, "Node"}, /* internal use: syntax tree node */
- {T_UNDEF, "undef"}, /* internal use: #undef; should not happen */
+static const char builtin_types[][10] = {
+ "", /* 0x00, */
+ "Object",
+ "Class",
+ "Module",
+ "Float",
+ "String",
+ "Regexp",
+ "Array",
+ "Hash",
+ "Struct",
+ "Bignum",
+ "File",
+ "Data", /* internal use: wrapped C pointers */
+ "MatchData", /* data of $~ */
+ "Complex",
+ "Rational",
+ "", /* 0x10 */
+ "nil",
+ "true",
+ "false",
+ "Symbol", /* :symbol */
+ "Fixnum",
+ "", /* 0x16 */
+ "", /* 0x17 */
+ "", /* 0x18 */
+ "", /* 0x19 */
+ {}, /* 0x1a */
+ "undef", /* internal use: #undef; should not happen */
+ "Node", /* internal use: syntax tree node */
+ "iClass", /* internal use: mixed-in module holder */
};
const char *
rb_builtin_type_name(int t)
{
- const struct types *type = builtin_types;
- const struct types *const typeend = builtin_types +
- sizeof(builtin_types) / sizeof(builtin_types[0]);
- while (type < typeend) {
- if (type->type == t) return type->name;
- type++;
- }
+ const char *name;
+ if ((unsigned int)t > numberof(builtin_types)) return 0;
+ name = builtin_types[t];
+ if (*name) return name;
return 0;
}