diff options
| author | Mike Pall <mike> | 2012-10-07 17:11:39 +0200 |
|---|---|---|
| committer | Mike Pall <mike> | 2012-10-07 17:11:39 +0200 |
| commit | 2f5ed5d0df518a08a52fc125473b17d3a46c2267 (patch) | |
| tree | c2ea38696fa52c2d1d65ca9c20a7552fbe2da011 /src | |
| parent | 0561a5693884d76db5b75f7cc746478b325b311b (diff) | |
| download | luajit-2f5ed5d0df518a08a52fc125473b17d3a46c2267.tar.gz luajit-2f5ed5d0df518a08a52fc125473b17d3a46c2267.tar.bz2 luajit-2f5ed5d0df518a08a52fc125473b17d3a46c2267.zip | |
From Lua 5.2: Add string.rep(s, n, sep).
Diffstat (limited to 'src')
| -rw-r--r-- | src/lib_string.c | 42 | ||||
| -rw-r--r-- | src/vm_arm.dasc | 2 | ||||
| -rw-r--r-- | src/vm_mips.dasc | 2 | ||||
| -rw-r--r-- | src/vm_ppc.dasc | 2 | ||||
| -rw-r--r-- | src/vm_ppcspe.dasc | 2 | ||||
| -rw-r--r-- | src/vm_x86.dasc | 3 |
6 files changed, 40 insertions, 13 deletions
diff --git a/src/lib_string.c b/src/lib_string.c index b36fcd6d..9aa74d5c 100644 --- a/src/lib_string.c +++ b/src/lib_string.c | |||
| @@ -86,22 +86,48 @@ LJLIB_ASM(string_sub) LJLIB_REC(string_range 1) | |||
| 86 | LJLIB_ASM(string_rep) | 86 | LJLIB_ASM(string_rep) |
| 87 | { | 87 | { |
| 88 | GCstr *s = lj_lib_checkstr(L, 1); | 88 | GCstr *s = lj_lib_checkstr(L, 1); |
| 89 | int32_t len = (int32_t)s->len; | ||
| 90 | int32_t k = lj_lib_checkint(L, 2); | 89 | int32_t k = lj_lib_checkint(L, 2); |
| 91 | int64_t tlen = (int64_t)k * len; | 90 | GCstr *sep = lj_lib_optstr(L, 3); |
| 91 | int32_t len = (int32_t)s->len; | ||
| 92 | global_State *g = G(L); | ||
| 93 | int64_t tlen; | ||
| 92 | const char *src; | 94 | const char *src; |
| 93 | char *buf; | 95 | char *buf; |
| 94 | if (k <= 0) return FFH_RETRY; | 96 | if (k <= 0) { |
| 95 | if (tlen > LJ_MAX_STR) | 97 | empty: |
| 96 | lj_err_caller(L, LJ_ERR_STROV); | 98 | setstrV(L, L->base-1, &g->strempty); |
| 97 | buf = lj_str_needbuf(L, &G(L)->tmpbuf, (MSize)tlen); | 99 | return FFH_RES(1); |
| 98 | if (len <= 1) return FFH_RETRY; /* ASM code only needed buffer resize. */ | 100 | } |
| 101 | if (sep) { | ||
| 102 | tlen = (int64_t)len + sep->len; | ||
| 103 | if (tlen > LJ_MAX_STR) | ||
| 104 | lj_err_caller(L, LJ_ERR_STROV); | ||
| 105 | tlen *= k; | ||
| 106 | if (tlen > LJ_MAX_STR) | ||
| 107 | lj_err_caller(L, LJ_ERR_STROV); | ||
| 108 | } else { | ||
| 109 | tlen = (int64_t)k * len; | ||
| 110 | if (tlen > LJ_MAX_STR) | ||
| 111 | lj_err_caller(L, LJ_ERR_STROV); | ||
| 112 | } | ||
| 113 | if (tlen == 0) goto empty; | ||
| 114 | buf = lj_str_needbuf(L, &g->tmpbuf, (MSize)tlen); | ||
| 99 | src = strdata(s); | 115 | src = strdata(s); |
| 116 | if (sep) { | ||
| 117 | tlen -= sep->len; /* Ignore trailing separator. */ | ||
| 118 | if (k > 1) { /* Paste one string and one separator. */ | ||
| 119 | int32_t i; | ||
| 120 | i = 0; while (i < len) *buf++ = src[i++]; | ||
| 121 | src = strdata(sep); len = sep->len; | ||
| 122 | i = 0; while (i < len) *buf++ = src[i++]; | ||
| 123 | src = g->tmpbuf.buf; len += s->len; k--; /* Now copy that k-1 times. */ | ||
| 124 | } | ||
| 125 | } | ||
| 100 | do { | 126 | do { |
| 101 | int32_t i = 0; | 127 | int32_t i = 0; |
| 102 | do { *buf++ = src[i++]; } while (i < len); | 128 | do { *buf++ = src[i++]; } while (i < len); |
| 103 | } while (--k > 0); | 129 | } while (--k > 0); |
| 104 | setstrV(L, L->base-1, lj_str_new(L, G(L)->tmpbuf.buf, (size_t)tlen)); | 130 | setstrV(L, L->base-1, lj_str_new(L, g->tmpbuf.buf, (size_t)tlen)); |
| 105 | return FFH_RES(1); | 131 | return FFH_RES(1); |
| 106 | } | 132 | } |
| 107 | 133 | ||
diff --git a/src/vm_arm.dasc b/src/vm_arm.dasc index 331a1b70..4909d827 100644 --- a/src/vm_arm.dasc +++ b/src/vm_arm.dasc | |||
| @@ -1778,7 +1778,7 @@ static void build_subroutines(BuildCtx *ctx) | |||
| 1778 | | ldrd CARG12, [BASE] | 1778 | | ldrd CARG12, [BASE] |
| 1779 | | ldrd CARG34, [BASE, #8] | 1779 | | ldrd CARG34, [BASE, #8] |
| 1780 | | cmp NARGS8:RC, #16 | 1780 | | cmp NARGS8:RC, #16 |
| 1781 | | blo ->fff_fallback | 1781 | | bne ->fff_fallback // Exactly 2 arguments |
| 1782 | | checktp CARG2, LJ_TSTR | 1782 | | checktp CARG2, LJ_TSTR |
| 1783 | | checktpeq CARG4, LJ_TISNUM | 1783 | | checktpeq CARG4, LJ_TISNUM |
| 1784 | | bne ->fff_fallback | 1784 | | bne ->fff_fallback |
diff --git a/src/vm_mips.dasc b/src/vm_mips.dasc index 719d9252..f82cefe8 100644 --- a/src/vm_mips.dasc +++ b/src/vm_mips.dasc | |||
| @@ -1696,7 +1696,7 @@ static void build_subroutines(BuildCtx *ctx) | |||
| 1696 | |.ffunc string_rep // Only handle the 1-char case inline. | 1696 | |.ffunc string_rep // Only handle the 1-char case inline. |
| 1697 | | ffgccheck | 1697 | | ffgccheck |
| 1698 | | lw TMP0, HI(BASE) | 1698 | | lw TMP0, HI(BASE) |
| 1699 | | sltiu AT, NARGS8:RC, 16 | 1699 | | addiu AT, NARGS8:RC, -16 // Exactly 2 arguments. |
| 1700 | | lw CARG4, 8+HI(BASE) | 1700 | | lw CARG4, 8+HI(BASE) |
| 1701 | | lw STR:CARG1, LO(BASE) | 1701 | | lw STR:CARG1, LO(BASE) |
| 1702 | | addiu TMP0, TMP0, -LJ_TSTR | 1702 | | addiu TMP0, TMP0, -LJ_TSTR |
diff --git a/src/vm_ppc.dasc b/src/vm_ppc.dasc index 6dbfb90d..7eafebe1 100644 --- a/src/vm_ppc.dasc +++ b/src/vm_ppc.dasc | |||
| @@ -2127,7 +2127,7 @@ static void build_subroutines(BuildCtx *ctx) | |||
| 2127 | |.else | 2127 | |.else |
| 2128 | | lfd FARG2, 8(BASE) | 2128 | | lfd FARG2, 8(BASE) |
| 2129 | |.endif | 2129 | |.endif |
| 2130 | | blt ->fff_fallback | 2130 | | bne ->fff_fallback // Exactly 2 arguments. |
| 2131 | | checkstr TMP0; bne ->fff_fallback | 2131 | | checkstr TMP0; bne ->fff_fallback |
| 2132 | |.if DUALNUM | 2132 | |.if DUALNUM |
| 2133 | | checknum CARG4; bne ->fff_fallback | 2133 | | checknum CARG4; bne ->fff_fallback |
diff --git a/src/vm_ppcspe.dasc b/src/vm_ppcspe.dasc index 2af06494..ab2d7670 100644 --- a/src/vm_ppcspe.dasc +++ b/src/vm_ppcspe.dasc | |||
| @@ -1630,7 +1630,7 @@ static void build_subroutines(BuildCtx *ctx) | |||
| 1630 | | cmplwi NARGS8:RC, 16 | 1630 | | cmplwi NARGS8:RC, 16 |
| 1631 | | evldd CARG1, 0(BASE) | 1631 | | evldd CARG1, 0(BASE) |
| 1632 | | evldd CARG2, 8(BASE) | 1632 | | evldd CARG2, 8(BASE) |
| 1633 | | blt ->fff_fallback | 1633 | | bne ->fff_fallback // Exactly 2 arguments. |
| 1634 | | checknum CARG2 | 1634 | | checknum CARG2 |
| 1635 | | checkfail ->fff_fallback | 1635 | | checkfail ->fff_fallback |
| 1636 | | checkstr STR:CARG1 | 1636 | | checkstr STR:CARG1 |
diff --git a/src/vm_x86.dasc b/src/vm_x86.dasc index 75683225..92e98f98 100644 --- a/src/vm_x86.dasc +++ b/src/vm_x86.dasc | |||
| @@ -2437,8 +2437,9 @@ static void build_subroutines(BuildCtx *ctx) | |||
| 2437 | | xor RC, RC // Zero length. Any ptr in RB is ok. | 2437 | | xor RC, RC // Zero length. Any ptr in RB is ok. |
| 2438 | | jmp <4 | 2438 | | jmp <4 |
| 2439 | | | 2439 | | |
| 2440 | |.ffunc_2 string_rep // Only handle the 1-char case inline. | 2440 | |.ffunc string_rep // Only handle the 1-char case inline. |
| 2441 | | ffgccheck | 2441 | | ffgccheck |
| 2442 | | cmp NARGS:RD, 2+1; jne ->fff_fallback // Exactly 2 arguments. | ||
| 2442 | | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback | 2443 | | cmp dword [BASE+4], LJ_TSTR; jne ->fff_fallback |
| 2443 | | cmp dword [BASE+12], LJ_TISNUM | 2444 | | cmp dword [BASE+12], LJ_TISNUM |
| 2444 | | mov STR:RB, [BASE] | 2445 | | mov STR:RB, [BASE] |
