From 427a6d8453dcd4d28d1de9c120a4a90c54c6776e Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 9 May 2024 14:44:40 +0000 Subject: [PATCH 1/2] =?UTF-8?q?str.pad=5Fstart=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + docs/primitive-props.md | 5 +++++ src/interpreter/primitive-props.ts | 7 +++++++ test/index.ts | 16 ++++++++++++++++ 4 files changed, 29 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5ef6934a..57e5b9db 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ # 未リリース分 - `Date:year`系の関数に0を渡すと現在時刻になる問題を修正 - シンタックスエラーなどの位置情報を修正 +- `str.pad_start`を追加 # 0.18.0 - `Core:abort`でプログラムを緊急停止できるように diff --git a/docs/primitive-props.md b/docs/primitive-props.md index 609cc6ee..90e57528 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -78,6 +78,11 @@ _fromIndex_が指定されていれば、その位置から検索を開始しま _fromIndex_が負値の時は末尾からの位置(文字列の長さ+_fromIndex_)が使用されます。 該当が無ければ-1を返します。 +### @(_v_: str).pad_start(_width_: num, _pad_?: str): str +文字列の長さがが _width_ になるように、先頭を _pad_ の繰り返しで埋めた新しい文字列を返します。\ +_pad_ を省略した場合、空白`' '`で埋められます。\ +_pad_ が長すぎる場合、_pad_ の末尾が切り捨てられます。 + ### @(_v_: str).trim(): str 文字列の前後の空白を取り除いたものを返します。 diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index c154c45a..2c68d8e9 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -119,6 +119,13 @@ const PRIMITIVE_PROPS: { const res = target.value.codePointAt(i.value) ?? target.value.charCodeAt(i.value); return Number.isNaN(res) ? NULL : NUM(res); }), + + pad_start: (target: VStr): VFn => FN_NATIVE(([width, pad], _) => { + assertNumber(width); + const s = (pad) ? (assertString(pad), pad.value) : ' '; + + return STR(target.value.padStart(width.value, s)); + }), }, arr: { diff --git a/test/index.ts b/test/index.ts index fed26c01..b290296e 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2626,6 +2626,22 @@ describe('primitive props', () => { ARR([NUM(97), NUM(98), NUM(99), NUM(240), NUM(169), NUM(184), NUM(189), NUM(240), NUM(159), NUM(145), NUM(137), NUM(240), NUM(159), NUM(143), NUM(191), NUM(240), NUM(159), NUM(145), NUM(168), NUM(226), NUM(128), NUM(141), NUM(240), NUM(159), NUM(145), NUM(166), NUM(100), NUM(101), NUM(102)]) ); }); + + test.concurrent("pad_start", async () => { + const res = await exe(` + let str = "abc" + <: [ + str.pad_start(0), str.pad_start(1), str.pad_start(2), str.pad_start(3), str.pad_start(4), str.pad_start(5), + str.pad_start(0, "0"), str.pad_start(1, "0"), str.pad_start(2, "0"), str.pad_start(3, "0"), str.pad_start(4, "0"), str.pad_start(5, "0"), + str.pad_start(0, "01"), str.pad_start(1, "01"), str.pad_start(2, "01"), str.pad_start(3, "01"), str.pad_start(4, "01"), str.pad_start(5, "01"), + ] + `); + eq(res, ARR([ + STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR(" abc"), STR(" abc"), + STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("0abc"), STR("00abc"), + STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("0abc"), STR("01abc"), + ])); + }); }); describe('arr', () => { From e1b3a22aacb4cd71ca3b60f48935c0b331c4cf64 Mon Sep 17 00:00:00 2001 From: salano_ym <53254905+salano-ym@users.noreply.github.com> Date: Thu, 9 May 2024 14:49:17 +0000 Subject: [PATCH 2/2] =?UTF-8?q?str.pad=5Fend=E3=82=92=E8=BF=BD=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 2 +- docs/primitive-props.md | 5 +++++ src/interpreter/primitive-props.ts | 7 +++++++ test/index.ts | 16 ++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 57e5b9db..385bde1f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ # 未リリース分 - `Date:year`系の関数に0を渡すと現在時刻になる問題を修正 - シンタックスエラーなどの位置情報を修正 -- `str.pad_start`を追加 +- `str.pad_start`,`str.pad_end`を追加 # 0.18.0 - `Core:abort`でプログラムを緊急停止できるように diff --git a/docs/primitive-props.md b/docs/primitive-props.md index 90e57528..1c71f5e9 100644 --- a/docs/primitive-props.md +++ b/docs/primitive-props.md @@ -83,6 +83,11 @@ _fromIndex_が負値の時は末尾からの位置(文字列の長さ+_fromInd _pad_ を省略した場合、空白`' '`で埋められます。\ _pad_ が長すぎる場合、_pad_ の末尾が切り捨てられます。 +### @(_v_: str).pad_end(_width_: num, _pad_?: str): str +文字列の長さがが _width_ になるように、末尾を _pad_ の繰り返しで埋めた新しい文字列を返します。\ +_pad_ を省略した場合、空白`' '`で埋められます。\ +_pad_ が長すぎる場合、_pad_ の末尾が切り捨てられます。 + ### @(_v_: str).trim(): str 文字列の前後の空白を取り除いたものを返します。 diff --git a/src/interpreter/primitive-props.ts b/src/interpreter/primitive-props.ts index 2c68d8e9..52a20104 100644 --- a/src/interpreter/primitive-props.ts +++ b/src/interpreter/primitive-props.ts @@ -126,6 +126,13 @@ const PRIMITIVE_PROPS: { return STR(target.value.padStart(width.value, s)); }), + + pad_end: (target: VStr): VFn => FN_NATIVE(([width, pad], _) => { + assertNumber(width); + const s = (pad) ? (assertString(pad), pad.value) : ' '; + + return STR(target.value.padEnd(width.value, s)); + }), }, arr: { diff --git a/test/index.ts b/test/index.ts index b290296e..93a51b0c 100644 --- a/test/index.ts +++ b/test/index.ts @@ -2642,6 +2642,22 @@ describe('primitive props', () => { STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("0abc"), STR("01abc"), ])); }); + + test.concurrent("pad_end", async () => { + const res = await exe(` + let str = "abc" + <: [ + str.pad_end(0), str.pad_end(1), str.pad_end(2), str.pad_end(3), str.pad_end(4), str.pad_end(5), + str.pad_end(0, "0"), str.pad_end(1, "0"), str.pad_end(2, "0"), str.pad_end(3, "0"), str.pad_end(4, "0"), str.pad_end(5, "0"), + str.pad_end(0, "01"), str.pad_end(1, "01"), str.pad_end(2, "01"), str.pad_end(3, "01"), str.pad_end(4, "01"), str.pad_end(5, "01"), + ] + `); + eq(res, ARR([ + STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("abc "), STR("abc "), + STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("abc0"), STR("abc00"), + STR("abc"), STR("abc"), STR("abc"), STR("abc"), STR("abc0"), STR("abc01"), + ])); + }); }); describe('arr', () => {