Hacker News new | ask | show | jobs
by hiker 2013 days ago
Maybe

  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;
  }
2 comments

Here's the same code in pascal.

    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;
And that would be faster than the Pascal version

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