lobject.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. ** $Id: lobject.c,v 1.44 2000/08/09 19:16:57 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdlib.h>
  8. #include "lua.h"
  9. #include "lobject.h"
  10. /*
  11. ** you can use the fact that the 3rd letter or each name is always different
  12. ** (e-m-r-b-n-l) to compare and switch these strings
  13. */
  14. const char *const luaO_typenames[] = { /* ORDER LUA_T */
  15. "userdata", "number", "string", "table", "function", "function", "nil",
  16. "function", "function"
  17. };
  18. const TObject luaO_nilobject = {TAG_NIL, {NULL}};
  19. /*
  20. ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2)
  21. */
  22. lint32 luaO_power2 (lint32 n) {
  23. lint32 p = MINPOWER2;
  24. while (p<=n) p<<=1;
  25. return p;
  26. }
  27. int luaO_equalObj (const TObject *t1, const TObject *t2) {
  28. if (ttype(t1) != ttype(t2)) return 0;
  29. switch (ttype(t1)) {
  30. case TAG_NUMBER:
  31. return nvalue(t1) == nvalue(t2);
  32. case TAG_STRING: case TAG_USERDATA:
  33. return tsvalue(t1) == tsvalue(t2);
  34. case TAG_TABLE:
  35. return hvalue(t1) == hvalue(t2);
  36. case TAG_CCLOSURE: case TAG_LCLOSURE:
  37. return clvalue(t1) == clvalue(t2);
  38. default:
  39. LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type");
  40. return 1; /* TAG_NIL */
  41. }
  42. }
  43. static double expten (unsigned int e) {
  44. double exp = 10.0;
  45. double res = 1.0;
  46. for (; e; e>>=1) {
  47. if (e & 1) res *= exp;
  48. exp *= exp;
  49. }
  50. return res;
  51. }
  52. int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */
  53. double a = 0.0;
  54. int point = 0; /* number of decimal digits */
  55. int sig;
  56. while (isspace((unsigned char)*s)) s++;
  57. sig = 0;
  58. switch (*s) {
  59. case '-': sig = 1; /* go through */
  60. case '+': s++;
  61. }
  62. if (! (isdigit((unsigned char)*s) ||
  63. (*s == '.' && isdigit((unsigned char)*(s+1)))))
  64. return 0; /* not (at least one digit before or after the point) */
  65. while (isdigit((unsigned char)*s))
  66. a = 10.0*a + (*(s++)-'0');
  67. if (*s == '.') {
  68. s++;
  69. while (isdigit((unsigned char)*s)) {
  70. a = 10.0*a + (*(s++)-'0');
  71. point++;
  72. }
  73. }
  74. if (sig) a = -a;
  75. if (*s == 'e' || *s == 'E') {
  76. int e = 0;
  77. s++;
  78. sig = 0;
  79. switch (*s) {
  80. case '-': sig = 1; /* go through */
  81. case '+': s++;
  82. }
  83. if (!isdigit((unsigned char)*s)) return 0; /* no digit in the exponent? */
  84. do {
  85. e = 10*e + (*(s++)-'0');
  86. } while (isdigit((unsigned char)*s));
  87. if (sig) e = -e;
  88. point -= e;
  89. }
  90. while (isspace((unsigned char)*s)) s++;
  91. if (*s != '\0') return 0; /* invalid trailing characters? */
  92. if (point != 0) {
  93. if (point > 0) a /= expten(point);
  94. else a *= expten(-point);
  95. }
  96. *result = a;
  97. return 1;
  98. }