lobject.c 2.5 KB

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