import type { Copy, char, Debug, Default, Display, Equal, From, IndexOutOfBoundsError, Integer, Into, Option, PartialEq, PositiveInteger, Result, SemiGroup, SerializeError } from '@chzky/core'; import type { Char, Vector } from '../../../mod.js'; import type { Str } from './class.js'; /** 可等效转化成`string`的数据结构 */ export type StringAble = string | { as_string(): string; }; /** 可搜索模式 */ export type SearchPattern = StringAble | RegExp; /** ## `StrConstant` : Str的静态部分 */ export interface StrConstant extends Default, SemiGroup, From | Array | StringAble | number, Str> { /** ## `Str` : UTF-8 编码的、可增长的字符串。 + `Str`的目的主要是扩展`string`的能力,使用一个包装类来实现`string`的扩展,而不是直接修改`string`的原型 */ (val: unknown): Str; /** ### `from` : `string`->`Str` */ from(str: Vector | Array | StringAble | number): Str; /** ### `from_utf8` : 将`Uint8Array`转为`Str` + 在遇到无效的UTF-8字节序列时,将返回{@linkcode SerializeError} */ from_utf8(bytes: Uint8Array): Result; /** ### `from_utf8_lossy` : 将`Uint8Array`转为`Str`,包括无效的UTF-8字节序列 + 使用`U+FFFD (�) `替换无效序列 */ from_utf8_lossy(bytes: Uint8Array): Str; /** ### `is_numeric` : 判断字符串是否能转化为数字 + 使用`Number()`构造函数进行数字化转换,如果转换失败(`NaN`),则返回`false` + 除了`NaN`之外的数字,包括`Infinity`和`-Infinity` @example check pass ```ts assert(Str.is_numeric('123'), '整数') assert(Str.is_numeric('-123'), '负数') assert(Str.is_numeric('123.45'), '浮点数') assert(Str.is_numeric('0'), '零') assert(Str.is_numeric(' 123 '), '带前导/尾随空格') assert(Str.is_numeric('1.23e+5'), '科学计数法') assert(Str.is_numeric('0xFF'), '十六进制') assert(Str.is_numeric(Str('42')), 'Str 实例') assert(Str.is_numeric('Infinity'), 'Infinity字符串') // 反向测试 (非数字字符串) assert.reverse(Str.is_numeric('abc'), '字母字符串') assert.reverse(Str.is_numeric('123a'), '混合数字字母') assert.reverse(Str.is_numeric(''), '空字符串') assert.reverse(Str.is_numeric(' '), '只包含空格的字符串') assert.reverse(Str.is_numeric('NaN'), 'NaN字符串') assert.reverse(Str.is_numeric('null'), 'null字符串') assert.reverse(Str.is_numeric('undefined'), 'undefined字符串') assert.reverse(Str.is_numeric('1,000'), '带逗号的数字字符串') ``` */ is_numeric(str: StringAble): boolean; /** ### `is_whitespace` : 判断字符串是否都是空白格/空字符 */ is_whitespace(str: StringAble): boolean; /** ### `encode` : 将`string`转为`Uint8Array` */ encode(str: StringAble): Uint8Array; /** ### `decode` : 将`Uint8Array`转为`Str` */ decode(bytes: Uint8Array): Str; /** ### `is` : 判断`Str`类型 */ is(value: unknown): value is Str; } export interface InterStr extends Into, Display, Debug, PartialEq, Copy, Equal { /** @convert */ /** ### `update` : 改变内部数据 */ update(str: StringAble): void; /** ### `as_string` : 返回`string` */ as_string(): string; /** ### `as_bytes` : 返回`Uint8Array` */ as_bytes(): Uint8Array; /** ### `to_chars` : 转化为`Array` */ to_chars(): Array; /** ### `to_vector` : 转化为`Vector` */ to_vector(): Vector; /** ### `chars` : 转化为`Array` */ chars(): Array; /** @methods */ /** ### `json_parse` : 进行Json反序列化 */ json_parse(): Result; /** ### `capacity` : 返回此字符串的容量(以字节为单位,utf8)。 */ capacity(): number; /** ### `clear` : `就地`清除字符串 @example ```ts const s = "hello" s.clear() s.is_empty() // true ``` */ clear(): void; /** ### `empty` : 返回此字符串是否为空。 */ is_empty(): boolean; /** ### `len` : 返回此字符串的长度。 */ len(): number; /** ### `find` : 返回此字符串中第一个出现的子字符串的位置。如果未找到子字符串,则返回 `None`。*/ find(substring: StringAble): Option; /** ### `rfind` : 返回此字符串中最后一个出现的子字符串的位置。如果未找到子字符串,则返回 `None`。*/ rfind(substring: StringAble): Option; /** ### `extend_from_within` : `就地`将元素从 src 范围复制到字符串的末尾 @example ```ts const s = "hello" s.extend_from_within([0, 1, 2, 4]).unwrap() s.to_string() // "hellohelo" ``` */ extend_from_within(src: Array | Generator | Vector): Result; /** ### `replace_range` : `就地`将字符串中指定范围内的字符替换为指定的字符串。 + 范围取值:`[start,end]` , 因此必须有字符被替换 @example 范围为正数 ```ts const s = "hello" s.replace_range("abc",0,1).unwrap() const v = s.to_string() // "abcllo" ``` @example 范围为负数 ```ts const s = "hello" s.replace_range("abc",-2,-1).unwrap() const v = s.tio_strng() // "helabc" ``` */ replace_range

(replace: StringAble, start: Integer

, end?: Integer): Result; /** ### `insert` : `就地`在指定位置插入字符串 + index的位置就是字符空白分隔的位置 : `(0/-5)h(1/-5)e(2/-3)l(3/-2)l(4/-1)o(5)` @example ```ts const s = "hello" s.insert(1,"").unwrap() const v = s.to_string() // “hello” v.insert(-2,"[insert]").unwrap() const v = s.to_string() // “hel[insert]lo” ``` */ insert

(index: Integer

, str: StringAble): Result; /** ### `remove` : `就地`删除并返回字符串指定位置字符 @example ```ts const s = "hello" s.remove(1).unwrap() // "e" const v = s.to_string() // “helo” ``` */ remove

(index: Integer

): Result; /** ### `push` : `就地`在字符串末尾插入字符串 */ push(str: StringAble): void; /** ### `unshift` : `就地`在字符串开头插入字符串 */ unshift(str: StringAble): void; /** ### `pop` : `就地`删除并返回字符串末尾字符 */ pop(): Option; /** ### `shift` : `就地`删除并返回字符串开头字符 */ shift(): Option; /** ### `remove_matches` : `就地`删除所有匹配<正则表达式/子字符串>的字符 */ remove_matches(pattern: RegExp | StringAble): void; /** ### `retain` : `就地`保留所有匹配`回调函数`的字符 @example ```ts const s = "hello" s.retain((c) => c !== "h") const v = s.to_string() // "ello" ``` */ retain(cb: (c: char) => boolean): void; /** ### `truncate` : `就地`将字符串截断到指定长度。 @example ```ts const s = "hello" s.truncate(2) const v = s.to_string() // "he" ``` */ truncate

(min: Integer

): void; /** ### `starts_with` : 检查此字符串是否以指定的子字符串开头。 */ start_with(str: StringAble): boolean; /** ### `ends_with` : 检查此字符串是否以指定的子字符串结尾。 */ end_with(str: StringAble): boolean; /** ### `contains` : 检查此字符串是否包含指定的子字符串。 */ contains(str: StringAble): boolean; /** ### `includes` : 检查此字符串是否包含指定的子字符串。 */ includes(str: StringAble): boolean; /** ### `is_ascii` : 检查此字符串是否为ASCII字符串。 */ is_ascii(): boolean; /** ### `make_ascii_lowercase` : `就地`将字符串转换为小写。 */ make_ascii_lowercase(): void; /** ### `make_ascii_uppercase` : `就地`将字符串转换为大写。 */ make_ascii_uppercase(): void; /** ### `lines` : 将`Str`依据行进行拆分 + 分行策略 : `\n` | `\r\n` @example ```ts const s = "hello\nworld\r\nNIC\r" const v = s.lines().map(l=>l.to_string()).collect() // ["hello", "world","NIC\r"] ``` */ lines(): Vector; /** ### `split_off` : `就地`将字符串根据`at`位置进行切分,返回被切分出的`Str`。 @example ```ts const s = "hello" const v = s.split_off(2).unwrap().to_string() // "lo" s.to_string() // "hel" ``` */ split_off

(at: Integer

): Result; /** @create_new */ /** ### `drain` : 返回一个包含此`Str`中指定偏移值字符的`Vector`。 + 该方法接受一个参数,即一个包含的index偏移值的数组,表示要提取的字符的内容。 @example ```ts const s = "hello world" const v = s.drain([0, 1, 2, 4]).collect() // [Char("h"), Char("e"), Char("l"), Char("o")] ``` */ drain(range: Array | Generator | Vector): Result, IndexOutOfBoundsError>; /** ### `get` : 返回此字符串中指定位置的字符。 + 如果位置越界则为`None` @example ```ts const s = "hello" const v = s.get([0, 1, 2, 4]).unwrap().to_string() // "helo" ``` */ get(range: Array | Generator | Vector): Option; /** ### `at` : 返回此字符串中指定位置的字符。 + 如果位置越界则为`None` @example ```ts const s = "hello" const v = s.at(0).unwrap().to_string() // "h" ``` */ at(index: number): Option; /** ### `strip_prefix` : 去除前缀 , 返回删除前缀后的`Str` + 如果不存在指定前缀则返回`None` @example ```ts const s = "hello" const v = s.strip_prefix("he").unwrap().to_string() // "llo" ``` */ strip_prefix(str: StringAble): Option; /** ### `strip_suffix` : 去除后缀 , 返回删除后缀后的`Str` + 如果不存在指定后缀则返回`None` @example ```ts const s = "hello" const v = s.strip_suffix("lo").unwrap().to_string() // "hel" ``` */ strip_suffix(str: string | Str): Option; /** ## `matches` : 匹配符合逻辑的字符串 @example ```ts const s = "hello" const v = s.matches(/l/).map(i=>i.to_string()).collect() // ["l", "l", "l"] ``` */ matches(pattern: SearchPattern | ((c: char) => boolean)): Vector; /** ### `repeat` : 重复字符串n次 */ repeat

(n: PositiveInteger

): Str; /** ### `replace` : 替换字符串中所有匹配的子字符串,返回一个新的`Str` */ replace(str: SearchPattern, replace: StringAble): Str; /** ### `replacen` : 替换字符串中`前n次`出现的子字符串,返回一个新的`Str` */ replacen(str: string | Str | RegExp, replace: StringAble, count: number): Str; /** ### `split` : 将`Str`依据pattern进行拆分。*/ split(pattern: SearchPattern): Vector; /** ### `split_at` : 将字符串根据`at`位置分割成两个`Str`。 @example ```ts const s = "hello" const v = s.split_at(2).unwrap().map(s=>s.to_string()).collect() // ["he", "llo"] ``` */ split_at

(at: Integer

): Result<[Str, Str], IndexOutOfBoundsError>; /** ### `split_inclusive` : 将`Str`依据pattern进行拆分。 + 与`split`不同的是,`split_inclusive`会将pattern也拆分出来 + 如果字符串 以分隔符结束,那么最后一个分隔符被处理后,剩余的部分就是一个空字符串,这个空字符串也会被作为最后一个元素生成。 @example 最后一个字符匹配不上 ```ts const s = "hello.world.xxx" const v = s.split_inclusive(".").map(s=>s.to_string()).collect() // ["hello.", , "world.","xxx"] ``` @example 最后一个字符匹配上 ```ts const s = "hello.world." const v = s.split_inclusive(".").map(s=>s.to_string()).collect() // ["hello.", "world.", ""] ``` @example 空字符串 ```ts parts = Str('') .split_inclusive('') .map(p => p.as_string()) .collect() assert.equal(parts, []) ``` */ split_inclusive(pattern: StringAble): Vector; /** ### `split_terminator` : 将`Str`依据`pattern`进行拆分。 和`split`不同的是,如果拆分后最后一个字符为空,`split_terminator`会进行忽略 @example ```ts // Use split_terminator const s = "hello.world." const v = s.split_terminator(".").map(s=>s.to_string()).collect() // ["hello", "world"] // Use split const s = "hello.world." const v = s.split(".").map(s=>s.to_string()).collect() // ["hello", "world", ""] ``` */ split_terminator(pattern: SearchPattern): Vector; /** ### `split_once` : 将`Str`依据pattern进行拆分,但是只拆一次。 + 如果不能拆分成两个则返回`None` */ split_once(pattern: SearchPattern): Option<[Str, Str]>; /** ### `split_ascii_whitespace` : 将`Str`依据**ASCII空格**进行拆分。 + ASCII空格有 : `\t,` `\n`, `\x0B`, `\x0C`, `\r`, ` ` */ split_ascii_whitespace(): Vector; /** ### `split_whitespace` : 将`Str`依据**Unicode空格**进行拆分。 + 如果只想按照ASCII空格拆分,请使用`split_ascii_whitespace` + Unicode定义的空格[见此](https://cloud.tencent.com/developer/article/2128593) */ split_whitespace(): Vector; /** ### `to_lowercase` : 将字符串转为小写 */ to_lowercase(): Str; /** ### `to_uppercase` : 将字符串转为大写 */ to_uppercase(): Str; /** ### `trim` : 去除字符串两端的空白字符 */ trim(): Str; /** ### `trim_left` : 去除字符串左端的空白字符 + 关于空白字符的定义:`ASCII空格` */ trim_left(): Str; /** ### `trim_right` : 去除字符串右端的空白字符 + 关于空白字符的定义:`ASCII空格` */ trim_right(): Str; /** ### `trim_start` : 去除字符串左端的空白字符(包括换行符) + 关于空白字符的定义:`Unicode空格` @example ```ts const s = " \nhello\n" const v = s.trim_start().to_string() // "hello\n" ``` */ trim_start(): Str; /** ### `trim_end` : 去除字符串右端的空白字符(包括换行符) + 关于空白字符的定义:`Unicode空格` @example ```ts const s = "hello\n \n" const v = s.trim_end().to_string() // "hello" ``` */ trim_end(): Str; clone(): Str; /** 字符迭代,每次迭代单char数值 */ [Symbol.iterator](): Iterator; } //# sourceMappingURL=interface.d.ts.map