lobject.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. ** $Id: lobject.c,v 1.99 2003/06/10 12:36:26 roberto Exp roberto $
  3. ** Some generic functions over Lua objects
  4. ** See Copyright Notice in lua.h
  5. */
  6. #include <ctype.h>
  7. #include <stdarg.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #define lobject_c
  11. #include "lua.h"
  12. #include "ldo.h"
  13. #include "lmem.h"
  14. #include "lobject.h"
  15. #include "lstate.h"
  16. #include "lstring.h"
  17. #include "lvm.h"
  18. /* function to convert a string to a lua_Number */
  19. #ifndef lua_str2number
  20. #define lua_str2number(s,p) strtod((s), (p))
  21. #endif
  22. const TValue luaO_nilobject = {LUA_TNIL, {NULL}};
  23. /*
  24. ** converts an integer to a "floating point byte", represented as
  25. ** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm)
  26. */
  27. int luaO_int2fb (unsigned int x) {
  28. int m = 0; /* mantissa */
  29. while (x >= (1<<3)) {
  30. x = (x+1) >> 1;
  31. m++;
  32. }
  33. return (m << 3) | cast(int, x);
  34. }
  35. int luaO_log2 (unsigned int x) {
  36. static const lu_byte log_2[256] = {
  37. 0,1,2,2,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
  38. 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
  39. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  40. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  41. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  42. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  43. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,
  44. 8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8,8
  45. };
  46. int l = -1;
  47. while (x >= 256) { l += 8; x >>= 8; }
  48. return l + log_2[x];
  49. }
  50. int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
  51. if (ttype(t1) != ttype(t2)) return 0;
  52. else switch (ttype(t1)) {
  53. case LUA_TNIL:
  54. return 1;
  55. case LUA_TNUMBER:
  56. return nvalue(t1) == nvalue(t2);
  57. case LUA_TBOOLEAN:
  58. return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
  59. case LUA_TLIGHTUSERDATA:
  60. return pvalue(t1) == pvalue(t2);
  61. default:
  62. lua_assert(iscollectable(t1));
  63. return gcvalue(t1) == gcvalue(t2);
  64. }
  65. }
  66. int luaO_str2d (const char *s, lua_Number *result) {
  67. char *endptr;
  68. lua_Number res = lua_str2number(s, &endptr);
  69. if (endptr == s) return 0; /* no conversion */
  70. while (isspace((unsigned char)(*endptr))) endptr++;
  71. if (*endptr != '\0') return 0; /* invalid trailing characters? */
  72. *result = res;
  73. return 1;
  74. }
  75. static void pushstr (lua_State *L, const char *str) {
  76. setsvalue2s(L, L->top, luaS_new(L, str));
  77. incr_top(L);
  78. }
  79. /* this function handles only `%d', `%c', %f, and `%s' formats */
  80. const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
  81. int n = 1;
  82. pushstr(L, "");
  83. for (;;) {
  84. const char *e = strchr(fmt, '%');
  85. if (e == NULL) break;
  86. setsvalue2s(L, L->top, luaS_newlstr(L, fmt, e-fmt));
  87. incr_top(L);
  88. switch (*(e+1)) {
  89. case 's':
  90. pushstr(L, va_arg(argp, char *));
  91. break;
  92. case 'c': {
  93. char buff[2];
  94. buff[0] = cast(char, va_arg(argp, int));
  95. buff[1] = '\0';
  96. pushstr(L, buff);
  97. break;
  98. }
  99. case 'd':
  100. setnvalue(L->top, cast(lua_Number, va_arg(argp, int)));
  101. incr_top(L);
  102. break;
  103. case 'f':
  104. setnvalue(L->top, cast(lua_Number, va_arg(argp, l_uacNumber)));
  105. incr_top(L);
  106. break;
  107. case '%':
  108. pushstr(L, "%");
  109. break;
  110. default: {
  111. char buff[3];
  112. buff[0] = '%';
  113. buff[1] = *(e+1);
  114. buff[2] = '\0';
  115. pushstr(L, buff);
  116. break;
  117. }
  118. }
  119. n += 2;
  120. fmt = e+2;
  121. }
  122. pushstr(L, fmt);
  123. luaV_concat(L, n+1, L->top - L->base - 1);
  124. L->top -= n;
  125. return svalue(L->top - 1);
  126. }
  127. const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) {
  128. const char *msg;
  129. va_list argp;
  130. va_start(argp, fmt);
  131. msg = luaO_pushvfstring(L, fmt, argp);
  132. va_end(argp);
  133. return msg;
  134. }
  135. void luaO_chunkid (char *out, const char *source, int bufflen) {
  136. if (*source == '=') {
  137. strncpy(out, source+1, bufflen); /* remove first char */
  138. out[bufflen-1] = '\0'; /* ensures null termination */
  139. }
  140. else { /* out = "source", or "...source" */
  141. if (*source == '@') {
  142. int l;
  143. source++; /* skip the `@' */
  144. bufflen -= sizeof(" `...' ");
  145. l = strlen(source);
  146. strcpy(out, "");
  147. if (l>bufflen) {
  148. source += (l-bufflen); /* get last part of file name */
  149. strcat(out, "...");
  150. }
  151. strcat(out, source);
  152. }
  153. else { /* out = [string "string"] */
  154. int len = strcspn(source, "\n"); /* stop at first newline */
  155. bufflen -= sizeof(" [string \"...\"] ");
  156. if (len > bufflen) len = bufflen;
  157. strcpy(out, "[string \"");
  158. if (source[len] != '\0') { /* must truncate? */
  159. strncat(out, source, len);
  160. strcat(out, "...");
  161. }
  162. else
  163. strcat(out, source);
  164. strcat(out, "\"]");
  165. }
  166. }
  167. }