The Battle for Wesnoth  1.15.2+dev
Classes | Macros | Typedefs | Enumerations | Functions | Variables
lstrlib.cpp File Reference
#include "lprefix.h"
#include <ctype.h>
#include <float.h>
#include <limits.h>
#include <locale.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
Include dependency graph for lstrlib.cpp:

Go to the source code of this file.

Classes

struct  MatchState
 
struct  GMatchState
 
struct  cD
 
union  Ftypes
 
struct  Header
 

Macros

#define lstrlib_c
 
#define LUA_LIB
 
#define LUA_MAXCAPTURES   32
 
#define uchar(c)   ((unsigned char)(c))
 
#define MAX_SIZET   ((size_t)(~(size_t)0))
 
#define MAXSIZE   (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))
 
#define CAP_UNFINISHED   (-1)
 
#define CAP_POSITION   (-2)
 
#define MAXCCALLS   200
 
#define L_ESC   '%'
 
#define SPECIALS   "^$*+?.([%-"
 
#define MAX_ITEM   (120 + l_mathlim(MAX_10_EXP))
 
#define FLAGS   "-+ #0"
 
#define MAX_FORMAT   32
 
#define LUAL_PACKPADBYTE   0x00
 
#define MAXINTSIZE   16
 
#define NB   CHAR_BIT
 
#define MC   ((1 << NB) - 1)
 
#define SZINT   ((int)sizeof(lua_Integer))
 
#define MAXALIGN   (offsetof(struct cD, u))
 

Typedefs

typedef struct MatchState MatchState
 
typedef struct GMatchState GMatchState
 
typedef union Ftypes Ftypes
 
typedef struct Header Header
 
typedef enum KOption KOption
 

Enumerations

enum  KOption {
  Kint, Kuint, Kfloat, Kchar,
  Kstring, Kzstr, Kpadding, Kpaddalign,
  Knop
}
 

Functions

static int str_len (lua_State *L)
 
static lua_Integer posrelat (lua_Integer pos, size_t len)
 
static int str_sub (lua_State *L)
 
static int str_reverse (lua_State *L)
 
static int str_lower (lua_State *L)
 
static int str_upper (lua_State *L)
 
static int str_rep (lua_State *L)
 
static int str_byte (lua_State *L)
 
static int str_char (lua_State *L)
 
static int writer (lua_State *L, const void *b, size_t size, void *B)
 
static int str_dump (lua_State *L)
 
static const char * match (MatchState *ms, const char *s, const char *p)
 
static int check_capture (MatchState *ms, int l)
 
static int capture_to_close (MatchState *ms)
 
static const char * classend (MatchState *ms, const char *p)
 
static int match_class (int c, int cl)
 
static int matchbracketclass (int c, const char *p, const char *ec)
 
static int singlematch (MatchState *ms, const char *s, const char *p, const char *ep)
 
static const char * matchbalance (MatchState *ms, const char *s, const char *p)
 
static const char * max_expand (MatchState *ms, const char *s, const char *p, const char *ep)
 
static const char * min_expand (MatchState *ms, const char *s, const char *p, const char *ep)
 
static const char * start_capture (MatchState *ms, const char *s, const char *p, int what)
 
static const char * end_capture (MatchState *ms, const char *s, const char *p)
 
static const char * match_capture (MatchState *ms, const char *s, int l)
 
static const char * lmemfind (const char *s1, size_t l1, const char *s2, size_t l2)
 
static void push_onecapture (MatchState *ms, int i, const char *s, const char *e)
 
static int push_captures (MatchState *ms, const char *s, const char *e)
 
static int nospecials (const char *p, size_t l)
 
static void prepstate (MatchState *ms, lua_State *L, const char *s, size_t ls, const char *p, size_t lp)
 
static void reprepstate (MatchState *ms)
 
static int str_find_aux (lua_State *L, int find)
 
static int str_find (lua_State *L)
 
static int str_match (lua_State *L)
 
static int gmatch_aux (lua_State *L)
 
static int gmatch (lua_State *L)
 
static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, const char *e)
 
static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, const char *e, int tr)
 
static int str_gsub (lua_State *L)
 
static void addquoted (luaL_Buffer *b, const char *s, size_t len)
 
static void checkdp (char *buff, int nb)
 
static void addliteral (lua_State *L, luaL_Buffer *b, int arg)
 
static const char * scanformat (lua_State *L, const char *strfrmt, char *form)
 
static void addlenmod (char *form, const char *lenmod)
 
static int str_format (lua_State *L)
 
static int digit (int c)
 
static int getnum (const char **fmt, int df)
 
static int getnumlimit (Header *h, const char **fmt, int df)
 
static void initheader (lua_State *L, Header *h)
 
static KOption getoption (Header *h, const char **fmt, int *size)
 
static KOption getdetails (Header *h, size_t totalsize, const char **fmt, int *psize, int *ntoalign)
 
static void packint (luaL_Buffer *b, lua_Unsigned n, int islittle, int size, int neg)
 
static void copywithendian (volatile char *dest, volatile const char *src, int size, int islittle)
 
static int str_pack (lua_State *L)
 
static int str_packsize (lua_State *L)
 
static lua_Integer unpackint (lua_State *L, const char *str, int islittle, int size, int issigned)
 
static int str_unpack (lua_State *L)
 
static void createmetatable (lua_State *L)
 
LUAMOD_API int luaopen_string (lua_State *L)
 

Variables

union {
   int   dummy
 
   char   little
 
nativeendian = {1}
 
static const luaL_Reg strlib []
 

Macro Definition Documentation

◆ CAP_POSITION

#define CAP_POSITION   (-2)

Definition at line 211 of file lstrlib.cpp.

Referenced by match(), and push_onecapture().

◆ CAP_UNFINISHED

#define CAP_UNFINISHED   (-1)

Definition at line 210 of file lstrlib.cpp.

Referenced by capture_to_close(), check_capture(), end_capture(), match(), and push_onecapture().

◆ FLAGS

#define FLAGS   "-+ #0"

Definition at line 900 of file lstrlib.cpp.

Referenced by scanformat().

◆ L_ESC

#define L_ESC   '%'

Definition at line 238 of file lstrlib.cpp.

Referenced by add_s(), classend(), match(), matchbracketclass(), singlematch(), and str_format().

◆ lstrlib_c

#define lstrlib_c

Definition at line 7 of file lstrlib.cpp.

◆ LUA_LIB

#define LUA_LIB

Definition at line 8 of file lstrlib.cpp.

◆ LUA_MAXCAPTURES

#define LUA_MAXCAPTURES   32

Definition at line 34 of file lstrlib.cpp.

Referenced by start_capture().

◆ LUAL_PACKPADBYTE

#define LUAL_PACKPADBYTE   0x00

Definition at line 1107 of file lstrlib.cpp.

Referenced by str_pack().

◆ MAX_FORMAT

#define MAX_FORMAT   32

Definition at line 905 of file lstrlib.cpp.

Referenced by str_format().

◆ MAX_ITEM

#define MAX_ITEM   (120 + l_mathlim(MAX_10_EXP))

Definition at line 896 of file lstrlib.cpp.

Referenced by addliteral(), and str_format().

◆ MAX_SIZET

#define MAX_SIZET   ((size_t)(~(size_t)0))

Definition at line 46 of file lstrlib.cpp.

◆ MAXALIGN

#define MAXALIGN   (offsetof(struct cD, u))

Definition at line 1136 of file lstrlib.cpp.

Referenced by getoption().

◆ MAXCCALLS

#define MAXCCALLS   200

Definition at line 234 of file lstrlib.cpp.

Referenced by prepstate(), and reprepstate().

◆ MAXINTSIZE

#define MAXINTSIZE   16

Definition at line 1111 of file lstrlib.cpp.

Referenced by getnumlimit().

◆ MAXSIZE

#define MAXSIZE   (sizeof(size_t) < sizeof(int) ? MAX_SIZET : (size_t)(INT_MAX))

Definition at line 48 of file lstrlib.cpp.

Referenced by getnum(), str_packsize(), and str_rep().

◆ MC

#define MC   ((1 << NB) - 1)

Definition at line 1117 of file lstrlib.cpp.

Referenced by packint(), and unpackint().

◆ NB

#define NB   CHAR_BIT

Definition at line 1114 of file lstrlib.cpp.

Referenced by packint(), str_pack(), and unpackint().

◆ SPECIALS

#define SPECIALS   "^$*+?.([%-"

Definition at line 239 of file lstrlib.cpp.

Referenced by nospecials().

◆ SZINT

#define SZINT   ((int)sizeof(lua_Integer))

Definition at line 1120 of file lstrlib.cpp.

Referenced by packint(), str_pack(), and unpackint().

◆ uchar

#define uchar (   c)    ((unsigned char)(c))

Typedef Documentation

◆ Ftypes

typedef union Ftypes Ftypes

◆ GMatchState

typedef struct GMatchState GMatchState

◆ Header

typedef struct Header Header

◆ KOption

typedef enum KOption KOption

◆ MatchState

typedef struct MatchState MatchState

Enumeration Type Documentation

◆ KOption

enum KOption
Enumerator
Kint 
Kuint 
Kfloat 
Kchar 
Kstring 
Kzstr 
Kpadding 
Kpaddalign 
Knop 

Definition at line 1163 of file lstrlib.cpp.

Function Documentation

◆ add_s()

static void add_s ( MatchState ms,
luaL_Buffer b,
const char *  s,
const char *  e 
)
static

◆ add_value()

static void add_value ( MatchState ms,
luaL_Buffer b,
const char *  s,
const char *  e,
int  tr 
)
static

◆ addlenmod()

static void addlenmod ( char *  form,
const char *  lenmod 
)
static

Definition at line 1006 of file lstrlib.cpp.

Referenced by str_format().

◆ addliteral()

static void addliteral ( lua_State L,
luaL_Buffer b,
int  arg 
)
static

◆ addquoted()

static void addquoted ( luaL_Buffer b,
const char *  s,
size_t  len 
)
static

Definition at line 908 of file lstrlib.cpp.

References l_sprintf, luaL_addchar, luaL_addstring(), and uchar.

Referenced by addliteral().

◆ capture_to_close()

static int capture_to_close ( MatchState ms)
static

◆ check_capture()

static int check_capture ( MatchState ms,
int  l 
)
static

◆ checkdp()

static void checkdp ( char *  buff,
int  nb 
)
static

Definition at line 934 of file lstrlib.cpp.

References lua_getlocaledecpoint.

Referenced by addliteral().

◆ classend()

static const char* classend ( MatchState ms,
const char *  p 
)
static

Definition at line 258 of file lstrlib.cpp.

References MatchState::L, L_ESC, luaL_error(), p, and MatchState::p_end.

Referenced by match().

◆ copywithendian()

static void copywithendian ( volatile char *  dest,
volatile const char *  src,
int  size,
int  islittle 
)
static

Definition at line 1316 of file lstrlib.cpp.

References nativeendian.

Referenced by str_pack(), and str_unpack().

◆ createmetatable()

static void createmetatable ( lua_State L)
static

◆ digit()

static int digit ( int  c)
static

Definition at line 1180 of file lstrlib.cpp.

Referenced by b_str2int(), and getnum().

◆ end_capture()

static const char* end_capture ( MatchState ms,
const char *  s,
const char *  p 
)
static

Definition at line 403 of file lstrlib.cpp.

References CAP_UNFINISHED, MatchState::capture, capture_to_close(), MatchState::init, MatchState::len, and match().

Referenced by match().

◆ getdetails()

static KOption getdetails ( Header h,
size_t  totalsize,
const char **  fmt,
int *  psize,
int *  ntoalign 
)
static

Definition at line 1268 of file lstrlib.cpp.

References getoption(), Kchar, Kpaddalign, Header::L, luaL_argerror(), and Header::maxalign.

Referenced by str_pack(), str_packsize(), and str_unpack().

◆ getnum()

static int getnum ( const char **  fmt,
int  df 
)
static

Definition at line 1182 of file lstrlib.cpp.

References a, digit(), and MAXSIZE.

Referenced by getnumlimit(), and getoption().

◆ getnumlimit()

static int getnumlimit ( Header h,
const char **  fmt,
int  df 
)
static

Definition at line 1199 of file lstrlib.cpp.

References getnum(), Header::L, luaL_error(), and MAXINTSIZE.

Referenced by getoption().

◆ getoption()

static KOption getoption ( Header h,
const char **  fmt,
int *  size 
)
static

◆ gmatch()

static int gmatch ( lua_State L)
static

◆ gmatch_aux()

static int gmatch_aux ( lua_State L)
static

◆ initheader()

static void initheader ( lua_State L,
Header h 
)
static

Definition at line 1211 of file lstrlib.cpp.

References Header::islittle, MatchState::L, Header::L, Header::maxalign, and nativeendian.

Referenced by str_pack(), str_packsize(), and str_unpack().

◆ lmemfind()

static const char* lmemfind ( const char *  s1,
size_t  l1,
const char *  s2,
size_t  l2 
)
static

Definition at line 528 of file lstrlib.cpp.

References MatchState::init.

Referenced by str_find_aux().

◆ luaopen_string()

LUAMOD_API int luaopen_string ( lua_State L)

Definition at line 1579 of file lstrlib.cpp.

References createmetatable(), and luaL_newlib.

Referenced by lua_kernel_base::lua_kernel_base().

◆ match()

static const char * match ( MatchState ms,
const char *  s,
const char *  p 
)
static

◆ match_capture()

static const char* match_capture ( MatchState ms,
const char *  s,
int  l 
)
static

Definition at line 414 of file lstrlib.cpp.

References MatchState::capture, check_capture(), MatchState::init, MatchState::len, and MatchState::src_end.

Referenced by match().

◆ match_class()

static int match_class ( int  c,
int  cl 
)
static

Definition at line 282 of file lstrlib.cpp.

Referenced by matchbracketclass(), and singlematch().

◆ matchbalance()

static const char* matchbalance ( MatchState ms,
const char *  s,
const char *  p 
)
static

Definition at line 341 of file lstrlib.cpp.

References b, e, MatchState::L, luaL_error(), p, MatchState::p_end, and MatchState::src_end.

Referenced by match().

◆ matchbracketclass()

static int matchbracketclass ( int  c,
const char *  p,
const char *  ec 
)
static

Definition at line 302 of file lstrlib.cpp.

References L_ESC, match_class(), and uchar.

Referenced by match(), and singlematch().

◆ max_expand()

static const char* max_expand ( MatchState ms,
const char *  s,
const char *  p,
const char *  ep 
)
static

Definition at line 361 of file lstrlib.cpp.

References i, match(), and singlematch().

Referenced by match().

◆ min_expand()

static const char* min_expand ( MatchState ms,
const char *  s,
const char *  p,
const char *  ep 
)
static

Definition at line 376 of file lstrlib.cpp.

References match(), and singlematch().

Referenced by match().

◆ nospecials()

static int nospecials ( const char *  p,
size_t  l 
)
static

Definition at line 580 of file lstrlib.cpp.

References SPECIALS.

Referenced by str_find_aux().

◆ packint()

static void packint ( luaL_Buffer b,
lua_Unsigned  n,
int  islittle,
int  size,
int  neg 
)
static

Definition at line 1295 of file lstrlib.cpp.

References i, luaL_addsize, luaL_prepbuffsize(), MC, NB, utf8::size(), and SZINT.

Referenced by str_pack().

◆ posrelat()

static lua_Integer posrelat ( lua_Integer  pos,
size_t  len 
)
static

Definition at line 63 of file lstrlib.cpp.

Referenced by str_byte(), str_find_aux(), str_sub(), and str_unpack().

◆ prepstate()

static void prepstate ( MatchState ms,
lua_State L,
const char *  s,
size_t  ls,
const char *  p,
size_t  lp 
)
static

◆ push_captures()

static int push_captures ( MatchState ms,
const char *  s,
const char *  e 
)
static

Definition at line 569 of file lstrlib.cpp.

References i, MatchState::L, MatchState::level, luaL_checkstack(), push_onecapture(), and s.

Referenced by add_value(), gmatch_aux(), and str_find_aux().

◆ push_onecapture()

static void push_onecapture ( MatchState ms,
int  i,
const char *  s,
const char *  e 
)
static

◆ reprepstate()

static void reprepstate ( MatchState ms)
static

Definition at line 601 of file lstrlib.cpp.

References MatchState::level, lua_assert, MatchState::matchdepth, and MAXCCALLS.

Referenced by gmatch_aux(), str_find_aux(), and str_gsub().

◆ scanformat()

static const char* scanformat ( lua_State L,
const char *  strfrmt,
char *  form 
)
static

Definition at line 981 of file lstrlib.cpp.

References FLAGS, luaL_error(), p, and uchar.

Referenced by str_format().

◆ singlematch()

static int singlematch ( MatchState ms,
const char *  s,
const char *  p,
const char *  ep 
)
static

Definition at line 325 of file lstrlib.cpp.

References c, L_ESC, match_class(), matchbracketclass(), MatchState::src_end, and uchar.

Referenced by match(), max_expand(), and min_expand().

◆ start_capture()

static const char* start_capture ( MatchState ms,
const char *  s,
const char *  p,
int  what 
)
static

◆ str_byte()

static int str_byte ( lua_State L)
static

◆ str_char()

static int str_char ( lua_State L)
static

◆ str_dump()

static int str_dump ( lua_State L)
static

◆ str_find()

static int str_find ( lua_State L)
static

Definition at line 654 of file lstrlib.cpp.

References str_find_aux().

◆ str_find_aux()

static int str_find_aux ( lua_State L,
int  find 
)
static

◆ str_format()

static int str_format ( lua_State L)
static

◆ str_gsub()

static int str_gsub ( lua_State L)
static

◆ str_len()

static int str_len ( lua_State L)
static

Definition at line 54 of file lstrlib.cpp.

References lua_pushinteger(), and luaL_checklstring().

◆ str_lower()

static int str_lower ( lua_State L)
static

Definition at line 96 of file lstrlib.cpp.

References b, i, luaL_buffinitsize(), luaL_checklstring(), luaL_pushresultsize(), p, s, and uchar.

◆ str_match()

static int str_match ( lua_State L)
static

Definition at line 659 of file lstrlib.cpp.

References str_find_aux().

◆ str_pack()

static int str_pack ( lua_State L)
static

◆ str_packsize()

static int str_packsize ( lua_State L)
static

◆ str_rep()

static int str_rep ( lua_State L)
static

◆ str_reverse()

static int str_reverse ( lua_State L)
static

Definition at line 84 of file lstrlib.cpp.

References b, i, luaL_buffinitsize(), luaL_checklstring(), luaL_pushresultsize(), p, and s.

◆ str_sub()

static int str_sub ( lua_State L)
static

◆ str_unpack()

static int str_unpack ( lua_State L)
static

◆ str_upper()

static int str_upper ( lua_State L)
static

Definition at line 109 of file lstrlib.cpp.

References b, i, luaL_buffinitsize(), luaL_checklstring(), luaL_pushresultsize(), p, s, and uchar.

◆ unpackint()

static lua_Integer unpackint ( lua_State L,
const char *  str,
int  islittle,
int  size,
int  issigned 
)
static

Definition at line 1450 of file lstrlib.cpp.

References i, luaL_error(), MC, NB, utf8::size(), and SZINT.

Referenced by str_unpack().

◆ writer()

static int writer ( lua_State L,
const void *  b,
size_t  size,
void *  B 
)
static

Variable Documentation

◆ dummy

int dummy

◆ little

char little

Definition at line 1126 of file lstrlib.cpp.

◆ nativeendian

const { ... } nativeendian

◆ strlib

const luaL_Reg strlib[]
static
Initial value:
= {
{"byte", str_byte},
{"char", str_char},
{"dump", str_dump},
{"find", str_find},
{"format", str_format},
{"gmatch", gmatch},
{"gsub", str_gsub},
{"len", str_len},
{"lower", str_lower},
{"match", str_match},
{"rep", str_rep},
{"reverse", str_reverse},
{"sub", str_sub},
{"upper", str_upper},
{"pack", str_pack},
{"packsize", str_packsize},
{"unpack", str_unpack},
{NULL, NULL}
}
static int str_dump(lua_State *L)
Definition: lstrlib.cpp:189
static int str_match(lua_State *L)
Definition: lstrlib.cpp:659
static int str_reverse(lua_State *L)
Definition: lstrlib.cpp:84
static int str_gsub(lua_State *L)
Definition: lstrlib.cpp:762
static int str_char(lua_State *L)
Definition: lstrlib.cpp:167
static int str_rep(lua_State *L)
Definition: lstrlib.cpp:122
static int str_byte(lua_State *L)
Definition: lstrlib.cpp:148
static int str_format(lua_State *L)
Definition: lstrlib.cpp:1016
static int str_pack(lua_State *L)
Definition: lstrlib.cpp:1330
static int str_packsize(lua_State *L)
Definition: lstrlib.cpp:1417
static int str_lower(lua_State *L)
Definition: lstrlib.cpp:96
static int str_unpack(lua_State *L)
Definition: lstrlib.cpp:1476
static int str_find(lua_State *L)
Definition: lstrlib.cpp:654
static int str_len(lua_State *L)
Definition: lstrlib.cpp:54
static int gmatch(lua_State *L)
Definition: lstrlib.cpp:689
static int str_upper(lua_State *L)
Definition: lstrlib.cpp:109
static int str_sub(lua_State *L)
Definition: lstrlib.cpp:70

Definition at line 1542 of file lstrlib.cpp.