Should be like, getinteger(const s : string; var startPos: sizeint):integer;
Pascal always has the best possible ideas, and then fucks up the implementation.
Confirmed by experiment.
Parsing 1,2,...1000 string 1000 times 0.355 seconds
Parsing 1,2,....10000 string 1000 times 42.835 seconds
A given string might have 1-10 parameters in it, so the actual execution time isn't going to be that bad. I always think in terms of those 10 Mhz 286s, and how much faster it is these days. ;-)
Obviously, if I were reading Gigabytes of text, I might optimize a bit.
int getint(const char **s) {
int res = 0;
for (; **s && isspace(**s); (*s)++);
for (; **s && isdigit(**s); (*s)++)
res = 10 * res + **s - '0';
return res;
} function getnum(var s : string):integer;
var
c : char;
value : integer;
begin
value := 0;
while (length(s) <> 0) and NOT(s[1] in ['0'..'9']) do
delete(s,1,1);
while (length(s) <> 0) and (s[1] in ['0'..'9']) do
begin
value := value * 10 + (ord(s[1])-ord('0'));
delete(s,1,1);
end;
getnum := value;
end;But it is unsafe. Someone might mistype * * s as *s and then bad things happen.
Pascal strings are basically memory safe. With range checking enabled, you can write whatever and it can only access characters in the string.
Ofc, then people disable the range checking in release builds to make it faster
Then the pointed-at pointer can be manipulated within the method body and changes will be visible on the outside as well, giving you both the numeric return value as well as the shortened string as the output. This is what the second argument of the strto{d,f,ld} function does: https://devdocs.io/c/string/byte/strtof
var/out variables are much safer, since the variable exists at the calling site , is valid and not null.
It is also much safer than C++'s & references. It is very subtle, but rather amazing if you think about it. Because C++ can return a & reference, it can return a reference to a value on the stack frame of the callee, which then becomes invalid. But the var/out variable always exists on caller's site, so it is always safe.
(at least if it a reference to the stack. a reference to the heap could be freed indirectly through another reference. )
Is returning references frequently done in C++? I have only seen it in ostream's << operator-like constructs where you want to chain multiple calls to the same object.
Another source of issues might be the creation of temporaries.
Yes, in index operator as well.
It is rather common to avoid copying returned objects. (perhaps it becomes less common with std::move ?)
For example, the [] operator on the standard collections returns a reference, so the item can be modified
There Pascal has another clever idea. You can overload [] for reading and writing separately. Then x[k] := v becomes x.setItem(k, v)
[Edit] For example, you can also insert into a string, making it longer, without having to worry about it.
[Edit] One of the nice things about pascal strings, and variable parameters, is that you can write functions like
getinteger(var s : string):integer;
Which literally pulls a sign plus digits from s, skipping spaces, until it hits a space, comma, etc... s gets shorter, you get the integer, and it just works.
I don't think that type of thing is even possible in C because of null terminated unmanaged strings.