Sfoglia il codice sorgente

corrects decimal point to follow current locale

Roberto Ierusalimschy 19 anni fa
parent
commit
16fd4abaf6
2 ha cambiato i file con 17 aggiunte e 3 eliminazioni
  1. 15 2
      llex.c
  2. 2 1
      llex.h

+ 15 - 2
llex.c

@@ -1,11 +1,12 @@
 /*
 /*
-** $Id: llex.c,v 2.12 2005/05/17 19:49:15 roberto Exp roberto $
+** $Id: llex.c,v 2.13 2005/11/08 19:45:14 roberto Exp roberto $
 ** Lexical Analyzer
 ** Lexical Analyzer
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
 
 
 
 
 #include <ctype.h>
 #include <ctype.h>
+#include <locale.h>
 #include <string.h>
 #include <string.h>
 
 
 #define llex_c
 #define llex_c
@@ -134,6 +135,8 @@ static void inclinenumber (LexState *ls) {
 
 
 
 
 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
 void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
+  struct lconv *cv = localeconv();
+  ls->decpoint = (cv ? cv->decimal_point[0] : '.');
   ls->L = L;
   ls->L = L;
   ls->lookahead.token = TK_EOS;  /* no look-ahead token */
   ls->lookahead.token = TK_EOS;  /* no look-ahead token */
   ls->z = z;
   ls->z = z;
@@ -163,6 +166,13 @@ static int check_next (LexState *ls, const char *set) {
 }
 }
 
 
 
 
+static void correctbuff (LexState *ls, char from, char to) {
+  int n = luaZ_bufflen(ls->buff);
+  char *p = luaZ_buffer(ls->buff);
+  while (n--)
+    if (p[n] == from) p[n] = to;
+}
+
 
 
 /* LUA_NUMBER */
 /* LUA_NUMBER */
 static void read_numeral (LexState *ls, SemInfo *seminfo) {
 static void read_numeral (LexState *ls, SemInfo *seminfo) {
@@ -177,8 +187,11 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) {
     }
     }
   }
   }
   save(ls, '\0');
   save(ls, '\0');
-  if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r))
+  correctbuff(ls, '.', ls->decpoint);  /* follow locale for decimal point */
+  if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
+    correctbuff(ls, ls->decpoint, '.');  /* undo change */
     luaX_lexerror(ls, "malformed number", TK_NUMBER);
     luaX_lexerror(ls, "malformed number", TK_NUMBER);
+  }
 }
 }
 
 
 
 

+ 2 - 1
llex.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: llex.h,v 1.54 2005/04/25 19:24:10 roberto Exp roberto $
+** $Id: llex.h,v 1.55 2005/06/06 13:30:25 roberto Exp roberto $
 ** Lexical Analyzer
 ** Lexical Analyzer
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -63,6 +63,7 @@ typedef struct LexState {
   ZIO *z;  /* input stream */
   ZIO *z;  /* input stream */
   Mbuffer *buff;  /* buffer for tokens */
   Mbuffer *buff;  /* buffer for tokens */
   TString *source;  /* current source name */
   TString *source;  /* current source name */
+  char decpoint;  /* locale decimal point */
 } LexState;
 } LexState;