Add StringView / substring by reference to core library. #56850
Labels
area-core-library
SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
library-core
type-enhancement
A request for a change that isn't a bug
DISCLAIMER > I was sent here from the Dart language repo
I think it would be nice having a way to do substring without copying the entire string, thereby decreasing a both memory and performence overhead.
Problem to solve
The problem that it solves is the fact that currently, the substring method copies the whole buffer of the string. This copying can potentially take a lot of time and allocate a lot of unnecessary resources, that have to be then collected. It can slow down the program significantly.
Definition
The
StringView
would have all the same definitions ofString
, but instead of copying the whole buffer, it would just reference to the original / base string with some offset, with adjusted length. Strings are already immutable by default, meaning there is no problem with sideeffects that would otherwise cause bugs.Motivation
The reason i brought it up was because i was doing some parsing, and i wanted to tokenize a string by continuously finding tokens and eating them from the start with
str.substring(token.end)
.It could also be used in conjuction with
RegExp
like^\s+
, so that the^
character refers to the start of the view, not start of the string. Currently:Other
The reason why i'm asking for a feature, and not implementing it myself, is because it is impossible to implement it as a user. I need to be able to use the
StringView
as a plain old string, for example in regex. At first i wanted to extend theString
class, but sinceString
isfinal
, i can't do it. I also can't do it with extensions, since i need to be able to use it as a normal string, and extensions can't override defined behaviour, that, for exampleRegExp
depends on.TLDR > since i can't implement it as a user, it needs to be implemented in the core library where
String
is defined.Edit
I am not generally a sucker for speed, but in this case using substring can have a drastic performence issues, that are IMO not all that difiicult to solve (at least not looking at the big picture). I'm also willing to trade off some speed for ease of writing, as long as the performence degradation is not really visible. That's why i don't mind using regex, despite it being a "slow" abstraction. I'm using regex because i really hate writing NFA from scratch (that's a really big hassle) which i would have to write either way and i can also optimize the regex expression to get some speed back.
The text was updated successfully, but these errors were encountered: