From 485b858c6f7a1a3d09f5fd06e2b32c2b72f7920f Mon Sep 17 00:00:00 2001 From: ASDAlexander77 Date: Wed, 28 Oct 2026 17:10:09 +0000 Subject: [PATCH] String: substr `"Mozilla".substr(2, 3)` - `length` characters from `start`, a negative start counting from the end - was missing ("Can't resolve property 'substr'"), on string and on the String wrapper (docs/not_impl/not_impl27.ts). Test: string_substr. Co-Authored-By: Claude Opus 5.5 --- src/lib.d.ts | 4 ++++ src/lib.ts | 26 ++++++++++++++++++++++++++ tests/string_substr.ts | 19 +++++++++++++++++++ 3 files changed, 49 insertions(+) create mode 100644 tests/string_substr.ts diff --git a/src/lib.d.ts b/src/lib.d.ts index d1482b1..481e2cd 100644 --- a/src/lib.d.ts +++ b/src/lib.d.ts @@ -340,6 +340,8 @@ declare namespace __String { function split(this: string, separator?: string | RegExp, limit?: int): string[]; + function substr(this: string, start: int, length?: int): string; + function substring(this: string, indexStart: int, indexEnd?: int): string; function startsWith(this: string, searchString: string, position?: int): boolean; @@ -416,6 +418,8 @@ declare class String { split(this: string, separator?: string | RegExp, limit?: int): string[]; + substr(this: string, start: int, length?: int): string; + substring(this: string, indexStart: int, indexEnd?: int): string; startsWith(this: string, searchString: string, position?: int); diff --git a/src/lib.ts b/src/lib.ts index 5697b54..f9bf5d9 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -1181,6 +1181,28 @@ namespace __String { return strncmp(Ref(this[position]), searchString, lensuffix) == 0; } + // `length` characters from `start`; a negative start counts from the end + export function substr(this: string, start: int, length: int = this.length): string { + if (start < 0) { + start = start + this.length; + if (start < 0) { + start = 0; + } + } else if (start >= this.length) { + return ""; + } + + if (length <= 0) { + return ""; + } + + if (length > this.length - start) { + length = this.length - start; + } + + return this.substring(start, start + length); + } + export function substring(this: string, indexStart: int, indexEnd: int = this.length): string { if (indexStart < 0) { indexStart = 0; @@ -1392,6 +1414,10 @@ export class String { return this.value.split(separator, limit); } + public substr(start: int, length?: int): string { + return this.value.substr(start, length); + } + public substring(indexStart: int, indexEnd?: int): string { return this.value.substring(indexStart, indexEnd); } diff --git a/tests/string_substr.ts b/tests/string_substr.ts new file mode 100644 index 0000000..a4d91fe --- /dev/null +++ b/tests/string_substr.ts @@ -0,0 +1,19 @@ +function main() { + const s = "Mozilla"; + + assert(s.substr(0, 1) == "M"); + assert(s.substr(1, 0) == ""); + assert(s.substr(-1, 1) == "a"); + assert(s.substr(1, -1) == ""); + assert(s.substr(-3) == "lla"); + assert(s.substr(1) == "ozilla"); + assert(s.substr(-20, 2) == "Mo"); + assert(s.substr(20, 2) == ""); + assert(s.substr(2, 100) == "zilla"); + + // through the String wrapper + const w = new String("Mozilla"); + assert(w.substr(2, 3) == "zil"); + + console.log("ALL DONE"); +}