misc/liblua/lstrlib.c
author alfadur
Fri, 13 Apr 2018 00:17:30 +0200
changeset 13321 23ade5604f8d
parent 10017 de822cd3df3a
permissions -rw-r--r--
Fix bullet trails overlapping when the pass through wrap or bounce world edge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     1
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     2
** $Id: lstrlib.c,v 1.132.1.4 2008/07/11 17:27:21 roberto Exp $
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     3
** Standard library for string operations and pattern-matching
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     4
** See Copyright Notice in lua.h
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     5
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     6
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     7
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     8
#include <ctype.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
     9
#include <stddef.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    10
#include <stdio.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    11
#include <stdlib.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    12
#include <string.h>
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    13
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    14
#define lstrlib_c
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    15
#define LUA_LIB
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    16
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    17
#include "lua.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    18
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    19
#include "lauxlib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    20
#include "lualib.h"
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    21
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    22
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    23
/* macro to `unsign' a character */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    24
#define uchar(c)        ((unsigned char)(c))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    25
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    26
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    27
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    28
static int str_len (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    29
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    30
  luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    31
  lua_pushinteger(L, l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    32
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    33
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    34
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    35
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    36
static ptrdiff_t posrelat (ptrdiff_t pos, size_t len) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    37
  /* relative string position: negative means back from end */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    38
  if (pos < 0) pos += (ptrdiff_t)len + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    39
  return (pos >= 0) ? pos : 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    40
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    41
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    42
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    43
static int str_sub (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    44
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    45
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    46
  ptrdiff_t start = posrelat(luaL_checkinteger(L, 2), l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    47
  ptrdiff_t end = posrelat(luaL_optinteger(L, 3, -1), l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    48
  if (start < 1) start = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    49
  if (end > (ptrdiff_t)l) end = (ptrdiff_t)l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    50
  if (start <= end)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    51
    lua_pushlstring(L, s+start-1, end-start+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    52
  else lua_pushliteral(L, "");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    53
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    54
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    55
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    56
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    57
static int str_reverse (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    58
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    59
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    60
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    61
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    62
  while (l--) luaL_addchar(&b, s[l]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    63
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    64
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    65
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    66
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    67
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    68
static int str_lower (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    69
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    70
  size_t i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    71
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    72
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    73
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    74
  for (i=0; i<l; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    75
    luaL_addchar(&b, tolower(uchar(s[i])));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    76
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    77
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    78
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    79
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    80
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    81
static int str_upper (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    82
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    83
  size_t i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    84
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    85
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    86
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    87
  for (i=0; i<l; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    88
    luaL_addchar(&b, toupper(uchar(s[i])));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    89
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    90
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    91
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    92
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    93
static int str_rep (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    94
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    95
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    96
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    97
  int n = luaL_checkint(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    98
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
    99
  while (n-- > 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   100
    luaL_addlstring(&b, s, l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   101
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   102
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   103
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   104
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   105
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   106
static int str_byte (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   107
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   108
  const char *s = luaL_checklstring(L, 1, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   109
  ptrdiff_t posi = posrelat(luaL_optinteger(L, 2, 1), l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   110
  ptrdiff_t pose = posrelat(luaL_optinteger(L, 3, posi), l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   111
  int n, i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   112
  if (posi <= 0) posi = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   113
  if ((size_t)pose > l) pose = l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   114
  if (posi > pose) return 0;  /* empty interval; return no values */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   115
  n = (int)(pose -  posi + 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   116
  if (posi + n <= pose)  /* overflow? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   117
    luaL_error(L, "string slice too long");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   118
  luaL_checkstack(L, n, "string slice too long");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   119
  for (i=0; i<n; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   120
    lua_pushinteger(L, uchar(s[posi+i-1]));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   121
  return n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   122
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   123
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   124
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   125
static int str_char (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   126
  int n = lua_gettop(L);  /* number of arguments */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   127
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   128
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   129
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   130
  for (i=1; i<=n; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   131
    int c = luaL_checkint(L, i);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   132
    luaL_argcheck(L, uchar(c) == c, i, "invalid value");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   133
    luaL_addchar(&b, uchar(c));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   134
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   135
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   136
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   137
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   138
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   139
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   140
static int writer (lua_State *L, const void* b, size_t size, void* B) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   141
  (void)L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   142
  luaL_addlstring((luaL_Buffer*) B, (const char *)b, size);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   143
  return 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   144
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   145
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   146
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   147
static int str_dump (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   148
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   149
  luaL_checktype(L, 1, LUA_TFUNCTION);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   150
  lua_settop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   151
  luaL_buffinit(L,&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   152
  if (lua_dump(L, writer, &b) != 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   153
    luaL_error(L, "unable to dump given function");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   154
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   155
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   156
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   157
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   158
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   159
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   160
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   161
** {======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   162
** PATTERN MATCHING
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   163
** =======================================================
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   164
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   165
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   166
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   167
#define CAP_UNFINISHED  (-1)
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   168
#define CAP_POSITION    (-2)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   169
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   170
typedef struct MatchState {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   171
  const char *src_init;  /* init of source string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   172
  const char *src_end;  /* end (`\0') of source string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   173
  lua_State *L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   174
  int level;  /* total number of captures (finished or unfinished) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   175
  struct {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   176
    const char *init;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   177
    ptrdiff_t len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   178
  } capture[LUA_MAXCAPTURES];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   179
} MatchState;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   180
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   181
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   182
#define L_ESC       '%'
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   183
#define SPECIALS    "^$*+?.([%-"
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   184
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   185
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   186
static int check_capture (MatchState *ms, int l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   187
  l -= '1';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   188
  if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   189
    return luaL_error(ms->L, "invalid capture index");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   190
  return l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   191
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   192
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   193
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   194
static int capture_to_close (MatchState *ms) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   195
  int level = ms->level;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   196
  for (level--; level>=0; level--)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   197
    if (ms->capture[level].len == CAP_UNFINISHED) return level;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   198
  return luaL_error(ms->L, "invalid pattern capture");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   199
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   200
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   201
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   202
static const char *classend (MatchState *ms, const char *p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   203
  switch (*p++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   204
    case L_ESC: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   205
      if (*p == '\0')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   206
        luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   207
      return p+1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   208
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   209
    case '[': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   210
      if (*p == '^') p++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   211
      do {  /* look for a `]' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   212
        if (*p == '\0')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   213
          luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   214
        if (*(p++) == L_ESC && *p != '\0')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   215
          p++;  /* skip escapes (e.g. `%]') */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   216
      } while (*p != ']');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   217
      return p+1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   218
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   219
    default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   220
      return p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   221
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   222
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   223
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   224
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   225
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   226
static int match_class (int c, int cl) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   227
  int res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   228
  switch (tolower(cl)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   229
    case 'a' : res = isalpha(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   230
    case 'c' : res = iscntrl(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   231
    case 'd' : res = isdigit(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   232
    case 'l' : res = islower(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   233
    case 'p' : res = ispunct(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   234
    case 's' : res = isspace(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   235
    case 'u' : res = isupper(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   236
    case 'w' : res = isalnum(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   237
    case 'x' : res = isxdigit(c); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   238
    case 'z' : res = (c == 0); break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   239
    default: return (cl == c);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   240
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   241
  return (islower(cl) ? res : !res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   242
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   243
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   244
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   245
static int matchbracketclass (int c, const char *p, const char *ec) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   246
  int sig = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   247
  if (*(p+1) == '^') {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   248
    sig = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   249
    p++;  /* skip the `^' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   250
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   251
  while (++p < ec) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   252
    if (*p == L_ESC) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   253
      p++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   254
      if (match_class(c, uchar(*p)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   255
        return sig;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   256
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   257
    else if ((*(p+1) == '-') && (p+2 < ec)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   258
      p+=2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   259
      if (uchar(*(p-2)) <= c && c <= uchar(*p))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   260
        return sig;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   261
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   262
    else if (uchar(*p) == c) return sig;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   263
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   264
  return !sig;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   265
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   266
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   267
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   268
static int singlematch (int c, const char *p, const char *ep) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   269
  switch (*p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   270
    case '.': return 1;  /* matches any char */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   271
    case L_ESC: return match_class(c, uchar(*(p+1)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   272
    case '[': return matchbracketclass(c, p, ep-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   273
    default:  return (uchar(*p) == c);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   274
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   275
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   276
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   277
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   278
static const char *match (MatchState *ms, const char *s, const char *p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   279
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   280
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   281
static const char *matchbalance (MatchState *ms, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   282
                                   const char *p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   283
  if (*p == 0 || *(p+1) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   284
    luaL_error(ms->L, "unbalanced pattern");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   285
  if (*s != *p) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   286
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   287
    int b = *p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   288
    int e = *(p+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   289
    int cont = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   290
    while (++s < ms->src_end) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   291
      if (*s == e) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   292
        if (--cont == 0) return s+1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   293
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   294
      else if (*s == b) cont++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   295
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   296
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   297
  return NULL;  /* string ends out of balance */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   298
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   299
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   300
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   301
static const char *max_expand (MatchState *ms, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   302
                                 const char *p, const char *ep) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   303
  ptrdiff_t i = 0;  /* counts maximum expand for item */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   304
  while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   305
    i++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   306
  /* keeps trying to match with the maximum repetitions */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   307
  while (i>=0) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   308
    const char *res = match(ms, (s+i), ep+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   309
    if (res) return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   310
    i--;  /* else didn't match; reduce 1 repetition to try again */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   311
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   312
  return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   313
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   314
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   315
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   316
static const char *min_expand (MatchState *ms, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   317
                                 const char *p, const char *ep) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   318
  for (;;) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   319
    const char *res = match(ms, s, ep+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   320
    if (res != NULL)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   321
      return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   322
    else if (s<ms->src_end && singlematch(uchar(*s), p, ep))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   323
      s++;  /* try with one more repetition */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   324
    else return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   325
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   326
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   327
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   328
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   329
static const char *start_capture (MatchState *ms, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   330
                                    const char *p, int what) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   331
  const char *res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   332
  int level = ms->level;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   333
  if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   334
  ms->capture[level].init = s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   335
  ms->capture[level].len = what;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   336
  ms->level = level+1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   337
  if ((res=match(ms, s, p)) == NULL)  /* match failed? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   338
    ms->level--;  /* undo capture */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   339
  return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   340
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   341
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   342
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   343
static const char *end_capture (MatchState *ms, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   344
                                  const char *p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   345
  int l = capture_to_close(ms);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   346
  const char *res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   347
  ms->capture[l].len = s - ms->capture[l].init;  /* close capture */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   348
  if ((res = match(ms, s, p)) == NULL)  /* match failed? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   349
    ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   350
  return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   351
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   352
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   353
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   354
static const char *match_capture (MatchState *ms, const char *s, int l) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   355
  size_t len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   356
  l = check_capture(ms, l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   357
  len = ms->capture[l].len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   358
  if ((size_t)(ms->src_end-s) >= len &&
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   359
      memcmp(ms->capture[l].init, s, len) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   360
    return s+len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   361
  else return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   362
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   363
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   364
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   365
static const char *match (MatchState *ms, const char *s, const char *p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   366
  init: /* using goto's to optimize tail recursion */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   367
  switch (*p) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   368
    case '(': {  /* start capture */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   369
      if (*(p+1) == ')')  /* position capture? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   370
        return start_capture(ms, s, p+2, CAP_POSITION);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   371
      else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   372
        return start_capture(ms, s, p+1, CAP_UNFINISHED);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   373
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   374
    case ')': {  /* end capture */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   375
      return end_capture(ms, s, p+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   376
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   377
    case L_ESC: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   378
      switch (*(p+1)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   379
        case 'b': {  /* balanced string? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   380
          s = matchbalance(ms, s, p+2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   381
          if (s == NULL) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   382
          p+=4; goto init;  /* else return match(ms, s, p+4); */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   383
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   384
        case 'f': {  /* frontier? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   385
          const char *ep; char previous;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   386
          p += 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   387
          if (*p != '[')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   388
            luaL_error(ms->L, "missing " LUA_QL("[") " after "
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   389
                               LUA_QL("%%f") " in pattern");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   390
          ep = classend(ms, p);  /* points to what is next */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   391
          previous = (s == ms->src_init) ? '\0' : *(s-1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   392
          if (matchbracketclass(uchar(previous), p, ep-1) ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   393
             !matchbracketclass(uchar(*s), p, ep-1)) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   394
          p=ep; goto init;  /* else return match(ms, s, ep); */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   395
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   396
        default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   397
          if (isdigit(uchar(*(p+1)))) {  /* capture results (%0-%9)? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   398
            s = match_capture(ms, s, uchar(*(p+1)));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   399
            if (s == NULL) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   400
            p+=2; goto init;  /* else return match(ms, s, p+2) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   401
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   402
          goto dflt;  /* case default */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   403
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   404
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   405
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   406
    case '\0': {  /* end of pattern */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   407
      return s;  /* match succeeded */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   408
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   409
    case '$': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   410
      if (*(p+1) == '\0')  /* is the `$' the last char in pattern? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   411
        return (s == ms->src_end) ? s : NULL;  /* check end of string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   412
      else goto dflt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   413
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   414
    default: dflt: {  /* it is a pattern item */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   415
      const char *ep = classend(ms, p);  /* points to what is next */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   416
      int m = s<ms->src_end && singlematch(uchar(*s), p, ep);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   417
      switch (*ep) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   418
        case '?': {  /* optional */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   419
          const char *res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   420
          if (m && ((res=match(ms, s+1, ep+1)) != NULL))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   421
            return res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   422
          p=ep+1; goto init;  /* else return match(ms, s, ep+1); */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   423
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   424
        case '*': {  /* 0 or more repetitions */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   425
          return max_expand(ms, s, p, ep);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   426
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   427
        case '+': {  /* 1 or more repetitions */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   428
          return (m ? max_expand(ms, s+1, p, ep) : NULL);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   429
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   430
        case '-': {  /* 0 or more repetitions (minimum) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   431
          return min_expand(ms, s, p, ep);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   432
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   433
        default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   434
          if (!m) return NULL;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   435
          s++; p=ep; goto init;  /* else return match(ms, s+1, ep); */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   436
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   437
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   438
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   439
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   440
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   441
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   442
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   443
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   444
static const char *lmemfind (const char *s1, size_t l1,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   445
                               const char *s2, size_t l2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   446
  if (l2 == 0) return s1;  /* empty strings are everywhere */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   447
  else if (l2 > l1) return NULL;  /* avoids a negative `l1' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   448
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   449
    const char *init;  /* to search for a `*s2' inside `s1' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   450
    l2--;  /* 1st char will be checked by `memchr' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   451
    l1 = l1-l2;  /* `s2' cannot be found after that */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   452
    while (l1 > 0 && (init = (const char *)memchr(s1, *s2, l1)) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   453
      init++;   /* 1st char is already checked */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   454
      if (memcmp(init, s2+1, l2) == 0)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   455
        return init-1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   456
      else {  /* correct `l1' and `s1' to try again */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   457
        l1 -= init-s1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   458
        s1 = init;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   459
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   460
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   461
    return NULL;  /* not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   462
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   463
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   464
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   465
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   466
static void push_onecapture (MatchState *ms, int i, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   467
                                                    const char *e) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   468
  if (i >= ms->level) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   469
    if (i == 0)  /* ms->level == 0, too */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   470
      lua_pushlstring(ms->L, s, e - s);  /* add whole match */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   471
    else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   472
      luaL_error(ms->L, "invalid capture index");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   473
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   474
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   475
    ptrdiff_t l = ms->capture[i].len;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   476
    if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   477
    if (l == CAP_POSITION)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   478
      lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   479
    else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   480
      lua_pushlstring(ms->L, ms->capture[i].init, l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   481
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   482
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   483
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   484
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   485
static int push_captures (MatchState *ms, const char *s, const char *e) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   486
  int i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   487
  int nlevels = (ms->level == 0 && s) ? 1 : ms->level;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   488
  luaL_checkstack(ms->L, nlevels, "too many captures");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   489
  for (i = 0; i < nlevels; i++)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   490
    push_onecapture(ms, i, s, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   491
  return nlevels;  /* number of strings pushed */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   492
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   493
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   494
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   495
static int str_find_aux (lua_State *L, int find) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   496
  size_t l1, l2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   497
  const char *s = luaL_checklstring(L, 1, &l1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   498
  const char *p = luaL_checklstring(L, 2, &l2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   499
  ptrdiff_t init = posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   500
  if (init < 0) init = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   501
  else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   502
  if (find && (lua_toboolean(L, 4) ||  /* explicit request? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   503
      strpbrk(p, SPECIALS) == NULL)) {  /* or no special characters? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   504
    /* do a plain search */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   505
    const char *s2 = lmemfind(s+init, l1-init, p, l2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   506
    if (s2) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   507
      lua_pushinteger(L, s2-s+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   508
      lua_pushinteger(L, s2-s+l2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   509
      return 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   510
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   511
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   512
  else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   513
    MatchState ms;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   514
    int anchor = (*p == '^') ? (p++, 1) : 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   515
    const char *s1=s+init;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   516
    ms.L = L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   517
    ms.src_init = s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   518
    ms.src_end = s+l1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   519
    do {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   520
      const char *res;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   521
      ms.level = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   522
      if ((res=match(&ms, s1, p)) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   523
        if (find) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   524
          lua_pushinteger(L, s1-s+1);  /* start */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   525
          lua_pushinteger(L, res-s);   /* end */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   526
          return push_captures(&ms, NULL, 0) + 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   527
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   528
        else
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   529
          return push_captures(&ms, s1, res);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   530
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   531
    } while (s1++ < ms.src_end && !anchor);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   532
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   533
  lua_pushnil(L);  /* not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   534
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   535
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   536
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   537
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   538
static int str_find (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   539
  return str_find_aux(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   540
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   541
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   542
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   543
static int str_match (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   544
  return str_find_aux(L, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   545
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   546
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   547
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   548
static int gmatch_aux (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   549
  MatchState ms;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   550
  size_t ls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   551
  const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   552
  const char *p = lua_tostring(L, lua_upvalueindex(2));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   553
  const char *src;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   554
  ms.L = L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   555
  ms.src_init = s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   556
  ms.src_end = s+ls;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   557
  for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   558
       src <= ms.src_end;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   559
       src++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   560
    const char *e;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   561
    ms.level = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   562
    if ((e = match(&ms, src, p)) != NULL) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   563
      lua_Integer newstart = e-s;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   564
      if (e == src) newstart++;  /* empty match? go at least one position */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   565
      lua_pushinteger(L, newstart);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   566
      lua_replace(L, lua_upvalueindex(3));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   567
      return push_captures(&ms, src, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   568
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   569
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   570
  return 0;  /* not found */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   571
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   572
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   573
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   574
static int gmatch (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   575
  luaL_checkstring(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   576
  luaL_checkstring(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   577
  lua_settop(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   578
  lua_pushinteger(L, 0);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   579
  lua_pushcclosure(L, gmatch_aux, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   580
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   581
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   582
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   583
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   584
static int gfind_nodef (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   585
  return luaL_error(L, LUA_QL("string.gfind") " was renamed to "
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   586
                       LUA_QL("string.gmatch"));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   587
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   588
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   589
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   590
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   591
                                                   const char *e) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   592
  size_t l, i;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   593
  const char *news = lua_tolstring(ms->L, 3, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   594
  for (i = 0; i < l; i++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   595
    if (news[i] != L_ESC)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   596
      luaL_addchar(b, news[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   597
    else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   598
      i++;  /* skip ESC */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   599
      if (!isdigit(uchar(news[i])))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   600
        luaL_addchar(b, news[i]);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   601
      else if (news[i] == '0')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   602
          luaL_addlstring(b, s, e - s);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   603
      else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   604
        push_onecapture(ms, news[i] - '1', s, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   605
        luaL_addvalue(b);  /* add capture to accumulated result */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   606
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   607
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   608
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   609
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   610
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   611
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   612
static void add_value (MatchState *ms, luaL_Buffer *b, const char *s,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   613
                                                       const char *e) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   614
  lua_State *L = ms->L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   615
  switch (lua_type(L, 3)) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   616
    case LUA_TNUMBER:
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   617
    case LUA_TSTRING: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   618
      add_s(ms, b, s, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   619
      return;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   620
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   621
    case LUA_TFUNCTION: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   622
      int n;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   623
      lua_pushvalue(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   624
      n = push_captures(ms, s, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   625
      lua_call(L, n, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   626
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   627
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   628
    case LUA_TTABLE: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   629
      push_onecapture(ms, 0, s, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   630
      lua_gettable(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   631
      break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   632
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   633
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   634
  if (!lua_toboolean(L, -1)) {  /* nil or false? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   635
    lua_pop(L, 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   636
    lua_pushlstring(L, s, e - s);  /* keep original text */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   637
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   638
  else if (!lua_isstring(L, -1))
3697
d5b30d6373fc remove trailing spaces from end of line
koda
parents: 2812
diff changeset
   639
    luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1));
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   640
  luaL_addvalue(b);  /* add result to accumulator */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   641
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   642
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   643
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   644
static int str_gsub (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   645
  size_t srcl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   646
  const char *src = luaL_checklstring(L, 1, &srcl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   647
  const char *p = luaL_checkstring(L, 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   648
  int  tr = lua_type(L, 3);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   649
  int max_s = luaL_optint(L, 4, srcl+1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   650
  int anchor = (*p == '^') ? (p++, 1) : 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   651
  int n = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   652
  MatchState ms;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   653
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   654
  luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   655
                   tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   656
                      "string/function/table expected");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   657
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   658
  ms.L = L;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   659
  ms.src_init = src;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   660
  ms.src_end = src+srcl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   661
  while (n < max_s) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   662
    const char *e;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   663
    ms.level = 0;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   664
    e = match(&ms, src, p);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   665
    if (e) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   666
      n++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   667
      add_value(&ms, &b, src, e);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   668
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   669
    if (e && e>src) /* non empty match? */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   670
      src = e;  /* skip it */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   671
    else if (src < ms.src_end)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   672
      luaL_addchar(&b, *src++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   673
    else break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   674
    if (anchor) break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   675
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   676
  luaL_addlstring(&b, src, ms.src_end-src);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   677
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   678
  lua_pushinteger(L, n);  /* number of substitutions */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   679
  return 2;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   680
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   681
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   682
/* }====================================================== */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   683
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   684
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   685
/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   686
#define MAX_ITEM    512
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   687
/* valid flags in a format specification */
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   688
#define FLAGS   "-+ #0"
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   689
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   690
** maximum size of each format specification (such as '%-099.99d')
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   691
** (+10 accounts for %99.99x plus margin of error)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   692
*/
10017
de822cd3df3a fixwhitespace and dos2unix
koda
parents: 3697
diff changeset
   693
#define MAX_FORMAT  (sizeof(FLAGS) + sizeof(LUA_INTFRMLEN) + 10)
2812
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   694
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   695
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   696
static void addquoted (lua_State *L, luaL_Buffer *b, int arg) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   697
  size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   698
  const char *s = luaL_checklstring(L, arg, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   699
  luaL_addchar(b, '"');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   700
  while (l--) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   701
    switch (*s) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   702
      case '"': case '\\': case '\n': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   703
        luaL_addchar(b, '\\');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   704
        luaL_addchar(b, *s);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   705
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   706
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   707
      case '\r': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   708
        luaL_addlstring(b, "\\r", 2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   709
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   710
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   711
      case '\0': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   712
        luaL_addlstring(b, "\\000", 4);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   713
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   714
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   715
      default: {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   716
        luaL_addchar(b, *s);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   717
        break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   718
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   719
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   720
    s++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   721
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   722
  luaL_addchar(b, '"');
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   723
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   724
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   725
static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   726
  const char *p = strfrmt;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   727
  while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++;  /* skip flags */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   728
  if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   729
    luaL_error(L, "invalid format (repeated flags)");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   730
  if (isdigit(uchar(*p))) p++;  /* skip width */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   731
  if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   732
  if (*p == '.') {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   733
    p++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   734
    if (isdigit(uchar(*p))) p++;  /* skip precision */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   735
    if (isdigit(uchar(*p))) p++;  /* (2 digits at most) */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   736
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   737
  if (isdigit(uchar(*p)))
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   738
    luaL_error(L, "invalid format (width or precision too long)");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   739
  *(form++) = '%';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   740
  strncpy(form, strfrmt, p - strfrmt + 1);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   741
  form += p - strfrmt + 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   742
  *form = '\0';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   743
  return p;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   744
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   745
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   746
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   747
static void addintlen (char *form) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   748
  size_t l = strlen(form);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   749
  char spec = form[l - 1];
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   750
  strcpy(form + l - 1, LUA_INTFRMLEN);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   751
  form[l + sizeof(LUA_INTFRMLEN) - 2] = spec;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   752
  form[l + sizeof(LUA_INTFRMLEN) - 1] = '\0';
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   753
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   754
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   755
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   756
static int str_format (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   757
  int arg = 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   758
  size_t sfl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   759
  const char *strfrmt = luaL_checklstring(L, arg, &sfl);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   760
  const char *strfrmt_end = strfrmt+sfl;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   761
  luaL_Buffer b;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   762
  luaL_buffinit(L, &b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   763
  while (strfrmt < strfrmt_end) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   764
    if (*strfrmt != L_ESC)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   765
      luaL_addchar(&b, *strfrmt++);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   766
    else if (*++strfrmt == L_ESC)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   767
      luaL_addchar(&b, *strfrmt++);  /* %% */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   768
    else { /* format item */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   769
      char form[MAX_FORMAT];  /* to store the format (`%...') */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   770
      char buff[MAX_ITEM];  /* to store the formatted item */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   771
      arg++;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   772
      strfrmt = scanformat(L, strfrmt, form);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   773
      switch (*strfrmt++) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   774
        case 'c': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   775
          sprintf(buff, form, (int)luaL_checknumber(L, arg));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   776
          break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   777
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   778
        case 'd':  case 'i': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   779
          addintlen(form);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   780
          sprintf(buff, form, (LUA_INTFRM_T)luaL_checknumber(L, arg));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   781
          break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   782
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   783
        case 'o':  case 'u':  case 'x':  case 'X': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   784
          addintlen(form);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   785
          sprintf(buff, form, (unsigned LUA_INTFRM_T)luaL_checknumber(L, arg));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   786
          break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   787
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   788
        case 'e':  case 'E': case 'f':
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   789
        case 'g': case 'G': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   790
          sprintf(buff, form, (double)luaL_checknumber(L, arg));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   791
          break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   792
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   793
        case 'q': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   794
          addquoted(L, &b, arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   795
          continue;  /* skip the 'addsize' at the end */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   796
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   797
        case 's': {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   798
          size_t l;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   799
          const char *s = luaL_checklstring(L, arg, &l);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   800
          if (!strchr(form, '.') && l >= 100) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   801
            /* no precision and string is too long to be formatted;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   802
               keep original string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   803
            lua_pushvalue(L, arg);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   804
            luaL_addvalue(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   805
            continue;  /* skip the `addsize' at the end */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   806
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   807
          else {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   808
            sprintf(buff, form, s);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   809
            break;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   810
          }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   811
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   812
        default: {  /* also treat cases `pnLlh' */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   813
          return luaL_error(L, "invalid option " LUA_QL("%%%c") " to "
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   814
                               LUA_QL("format"), *(strfrmt - 1));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   815
        }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   816
      }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   817
      luaL_addlstring(&b, buff, strlen(buff));
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   818
    }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   819
  }
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   820
  luaL_pushresult(&b);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   821
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   822
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   823
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   824
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   825
static const luaL_Reg strlib[] = {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   826
  {"byte", str_byte},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   827
  {"char", str_char},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   828
  {"dump", str_dump},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   829
  {"find", str_find},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   830
  {"format", str_format},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   831
  {"gfind", gfind_nodef},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   832
  {"gmatch", gmatch},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   833
  {"gsub", str_gsub},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   834
  {"len", str_len},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   835
  {"lower", str_lower},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   836
  {"match", str_match},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   837
  {"rep", str_rep},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   838
  {"reverse", str_reverse},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   839
  {"sub", str_sub},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   840
  {"upper", str_upper},
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   841
  {NULL, NULL}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   842
};
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   843
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   844
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   845
static void createmetatable (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   846
  lua_createtable(L, 0, 1);  /* create metatable for strings */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   847
  lua_pushliteral(L, "");  /* dummy string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   848
  lua_pushvalue(L, -2);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   849
  lua_setmetatable(L, -2);  /* set string metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   850
  lua_pop(L, 1);  /* pop dummy string */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   851
  lua_pushvalue(L, -2);  /* string library... */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   852
  lua_setfield(L, -2, "__index");  /* ...is the __index metamethod */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   853
  lua_pop(L, 1);  /* pop metatable */
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   854
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   855
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   856
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   857
/*
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   858
** Open string library
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   859
*/
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   860
LUALIB_API int luaopen_string (lua_State *L) {
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   861
  luaL_register(L, LUA_STRLIBNAME, strlib);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   862
#if defined(LUA_COMPAT_GFIND)
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   863
  lua_getfield(L, -1, "gmatch");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   864
  lua_setfield(L, -2, "gfind");
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   865
#endif
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   866
  createmetatable(L);
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   867
  return 1;
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   868
}
0a24853de796 add liblua to sources for macosx
koda
parents:
diff changeset
   869