Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- `arr.insert`,`arr.remove`を追加
- `arr.sort`の処理を非同期的にして高速化
- `arr.flat`,`arr.flat_map`を追加
- `Uri:encode_full`, `Uri:encode_component`, `Uri:decode_full`, `Uri:decode_component`を追加

# 0.18.0
- `Core:abort`でプログラムを緊急停止できるように
Expand Down
27 changes: 22 additions & 5 deletions docs/std.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,27 +116,44 @@ _time_offset_ を渡していない場合はローカルのものを参照しま
型: `str`
改行コード(LF)です。

### #Str:lt(a: str, b: str): num
### @Str:lt(a: str, b: str): num
a < b ならば -1、a == b ならば 0、a > b ならば 1 を返します。
arr.sortの比較関数として使用できます。

### #Str:gt(a: str, b: str): num
### @Str:gt(a: str, b: str): num
a > b ならば -1、a == b ならば 0、a < b ならば 1 を返します。
arr.sortの比較関数として使用できます。

### #Str:from_codepoint(codepoint: num): str
### @Str:from_codepoint(codepoint: num): str
Unicodeのコードポイントから文字を生成します。

_codepoint_ は 0 以上、10FFFF<sub>16</sub> 以下である必要があります。

### #Str:from_unicode_codepoints(_codePoints_: `arr<num>`): str
### @Str:from_unicode_codepoints(_codePoints_: `arr<num>`): str
Unicodeのコードポイント列を表す数値の配列から文字を生成します。
_codePoints_の各要素は 0 以上、10FFFF<sub>16</sub> 以下である必要があります。

### #Str:from_utf8_bytes(_bytes_: `arr<num>`): str
### @Str:from_utf8_bytes(_bytes_: `arr<num>`): str
UTF-8のバイト列を表す数値の配列から文字を生成します。
_bytes_の各要素は 0 以上、255 以下である必要があります。

## :: Uri
### @Uri:encode_full(uri: str): str
uri をURIとしてエンコードした文字列を返します。以下の文字はエンコードされません。
`A-Z a-z 0-9 - _ . ! ~ * ' ( ) ; , / ? : @ & = + $ #`

### @Uri:encode_component(text: str): str
text をURI構成要素としてエンコードした文字列を返します。以下の文字はエンコードされません。
`A-Z a-z 0-9 - _ . ! ~ * ' ( )`

### @Uri:decode_full(encoded_uri: str): str
encoded_uri をエンコードされたURIとしてデコードした文字列を返します。
以下の文字に対応するエスケープシーケンスはデコードされません。
`; , / ? : @ & = + $ #`

### @Uri:decode_component(encoded_text: str): str
encoded_text をエンコードされたURI構成要素としてデコードした文字列を返します。

## :: Arr
### @Arr:create(_length_: num, _initial_?: value): arr
長さが`length`の配列を作成します。
Expand Down
22 changes: 22 additions & 0 deletions src/interpreter/lib/std.ts
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,28 @@ export const std: Record<string, Value> = {
}),
//#endregion

//#region Uri
'Uri:encode_full': FN_NATIVE(([v]) => {
assertString(v);
return STR(encodeURI(v.value));
}),

'Uri:encode_component': FN_NATIVE(([v]) => {
assertString(v);
return STR(encodeURIComponent(v.value));
}),

'Uri:decode_full': FN_NATIVE(([v]) => {
assertString(v);
return STR(decodeURI(v.value));
}),

'Uri:decode_component': FN_NATIVE(([v]) => {
assertString(v);
return STR(decodeURIComponent(v.value));
}),
//#endregion

//#region Arr
'Arr:create': FN_NATIVE(([length, initial]) => {
assertNumber(length);
Expand Down
30 changes: 30 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3269,6 +3269,36 @@ describe('std', () => {
});
});

describe('Uri', () => {
test.concurrent('encode_full', async () => {
const res = await exe(`
<: Uri:encode_full("https://example.com/?q=あいちゃん")
`);
eq(res, STR('https://example.com/?q=%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93'));
});

test.concurrent('encode_component', async () => {
const res = await exe(`
<: Uri:encode_component("https://example.com/?q=あいちゃん")
`);
eq(res, STR('https%3A%2F%2Fexample.com%2F%3Fq%3D%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93'));
});

test.concurrent('decode_full', async () => {
const res = await exe(`
<: Uri:decode_full("https%3A%2F%2Fexample.com%2F%3Fq%3D%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93")
`);
eq(res, STR('https%3A%2F%2Fexample.com%2F%3Fq%3Dあいちゃん'));
});

test.concurrent('decode_component', async () => {
const res = await exe(`
<: Uri:decode_component("https%3A%2F%2Fexample.com%2F%3Fq%3D%E3%81%82%E3%81%84%E3%81%A1%E3%82%83%E3%82%93")
`);
eq(res, STR('https://example.com/?q=あいちゃん'));
});
});

describe('Error', () => {
test.concurrent('create', async () => {
eq(
Expand Down
6 changes: 3 additions & 3 deletions translations/en/docs/std.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,19 +113,19 @@ Generates a numeric value from a hexadecimal string.
Type: `str`.
Newline code (LF).

### #Str:lt(a: str, b: str): num
### @Str:lt(a: str, b: str): num
Returns -1 if a < b,
0 if a == b,
or 1 if a > b.
Using this as a comparison function for `arr.sort`, the array is sorted in ascending lexicographic order.

### #Str:gt(a: str, b: str): num
### @Str:gt(a: str, b: str): num
Returns -1 if a > b,
0 if a == b,
or 1 if a < b.
Using this as the comparison function for `arr.sort`, the array is sorted in descending lexicographic order.

### #Str:from_codepoint(codepoint: num): str
### @Str:from_codepoint(codepoint: num): str
Generates character from unicode code point.
_codepoint_ must be greater than or equal to 0 and less than or equal to 10FFFFFF<sub>16</sub>.
Multiple arguments are not supported yet.
Expand Down