Recent

Author Topic: StrToInt in pure pascal.  (Read 20709 times)

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: StrToInt in pure pascal.
« Reply #30 on: January 17, 2022, 06:57:00 am »
Всё по той же ссылке. Я заменил файл. Когда будет всё готово, выложу на GitHub.

Eng:
All from the same link. I have replaced the file. When everything is ready, I will post it on GitHub.

переполнение можно обойти {$Q-} и {$R-} ???
Eng: overflow can be bypassed {$Q-} and {$R-} ???
« Last Edit: January 20, 2022, 12:00:07 am by Seenkao »
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

kapibara

  • Hero Member
  • *****
  • Posts: 610
Re: StrToInt in pure pascal.
« Reply #31 on: January 17, 2022, 07:20:55 am »
Thanks, I'll check it later today.

(The attached archive in your first message in this thread still has the old version)
Lazarus trunk / fpc 3.2.2 / Kubuntu 22.04 - 64 bit

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: StrToInt in pure pascal.
« Reply #32 on: January 17, 2022, 10:41:59 am »
Thanks, I'll check it later today.

(The attached archive in your first message in this thread still has the old version)
Благодарю! Я не обратил внимания изначально. Это моя вина.
Исправил.

Eng:
Thanks to! I didn't pay attention initially. It's my fault.
Corrected.
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StrToInt in pure pascal.
« Reply #33 on: January 17, 2022, 02:49:08 pm »
Eng: overflow can be bypassed {$Q-} and {$R-} ???

Yes. Just enclose them with push and pop:
Code: Pascal  [Select][+][-]
  1. {$push}{$R-}
  2. The offending command goes here
  3. {$pop}
  4.  

duzenko

  • New Member
  • *
  • Posts: 41
Re: StrToInt in pure pascal.
« Reply #34 on: January 17, 2022, 03:46:17 pm »
I'd post this on stackoverflow
If I'd really needed a faster version of StrToInt, I'd look for it there

Kays

  • Hero Member
  • *****
  • Posts: 569
  • Whasup!?
    • KaiBurghardt.de
Re: StrToInt in pure pascal.
« Reply #35 on: January 17, 2022, 05:00:02 pm »
[…] Val() for unsigend integers is still broken though (it always expects the return value to be 32 or 64-bit, so it generates range check errors if you do Val('999',ByteVariable,Code), instead of just setting Code to 3 in this case).
Maybe that’s by design? Because there’s always readStr if you want it to fail:
Code: Pascal  [Select][+][-]
  1. program unsignedConversion(input, output, stdErr);
  2. {$rangeChecks on}
  3. var
  4.         b: Byte;
  5. begin
  6.         readStr('999', b); { causes RTE 201 “range check orror” }
  7. end.
Yours Sincerely
Kai Burghardt

Bart

  • Hero Member
  • *****
  • Posts: 5275
    • Bart en Mariska's Webstek
Re: StrToInt in pure pascal.
« Reply #36 on: January 17, 2022, 06:27:50 pm »
[…] Val() for unsigend integers is still broken though (it always expects the return value to be 32 or 64-bit, so it generates range check errors if you do Val('999',ByteVariable,Code), instead of just setting Code to 3 in this case).
Maybe that’s by design?

I hardly think so.
Nor do the fpc devels.
Hence I fixed it.
(It's in a separate branch, but will be merged to trunk soo I think.)

Bart

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: StrToInt in pure pascal.
« Reply #37 on: January 18, 2022, 02:01:40 pm »
I'd post this on stackoverflow
If I'd really needed a faster version of StrToInt, I'd look for it there
Вы должны понимать, что кто-то не пользуется stackoverflow (например я). Но вы можете сами предоставить решение там, указав ссылку на решение. Так же, код не проверялся на Turbo Pascal, Delphi, Pascal ABC и в таких ситуация могут предъявить претензии по компиляции кода.
Но, могут так же подсакать определённые решения для подобных проблем.  :)

google translate:
You must understand that someone does not use stackoverflow (like me). But you can provide the solution there yourself by providing a link to the solution. Also, the code has not been tested on Turbo Pascal, Delphi, Pascal ABC, and in such situations, claims can be made for compiling the code.
But, certain solutions for similar problems can also come up. :)

engkin, благодарю! Я посмотрю, чтоб код не выдавал ошибку при включенных флагах и переделаю его.
google translate:
engkin, thank you! I will see that the code does not throw an error when the flags are on and redo it.

Code: Pascal  [Select][+][-]
  1. {$push}{$R-}
  2. The offending command goes here
  3. {$pop}
And so?
Code: Pascal  [Select][+][-]
  1. {$R-}
  2. The offending command goes here
  3. {$R+}
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StrToInt in pure pascal.
« Reply #38 on: January 18, 2022, 02:39:41 pm »
And so?
Code: Pascal  [Select][+][-]
  1. {$R-}
  2. The offending command goes here
  3. {$R+}

Here your code will turn range checks on for any code after your code even if range checks were off in the project settings and before your code..

Zoran

  • Hero Member
  • *****
  • Posts: 1829
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: StrToInt in pure pascal.
« Reply #39 on: January 18, 2022, 03:08:09 pm »
Here your code will turn range checks on for any code after your code even if range checks were off in the project settings and before your code..

Yes, make a habit of using {$pop} and {$push}, it's a really good practice.

Also, about {$R} and {$Q} directives, I always use both -- either turn both on or off. Read this: https://wiki.freepascal.org/local_compiler_directives#.24R_versus_.24Q

Code: Pascal  [Select][+][-]
  1. {$push}
  2. {$R-}{$Q-}
  3. The offending command goes here
  4. {$pop}
  5.  

Thaddy

  • Hero Member
  • *****
  • Posts: 14203
  • Probably until I exterminate Putin.
Re: StrToInt in pure pascal.
« Reply #40 on: January 18, 2022, 03:17:06 pm »
Yes, make a habit of using {$pop} and {$push}, it's a really good practice.
Totally agree. Note that for Delphi compatible code this takes more effort.
Specialize a type, not a var.

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: StrToInt in pure pascal.
« Reply #41 on: January 18, 2022, 08:04:17 pm »
Благодарю за информацию!  :) Про "push" и "pop" я должен был догадаться... ведь в самом деле состояние могло быть изначально не включено.

Вопрос другой, если я включу это в блок процедуры или вообще на весь модуль - это сработает? Не будет ли состояние неизменным (если я отключил его) при вызове "Exit"? Или вернётся в прежнее состояние?

Точнее мне надо отделить команды определённые или я могу отделить полностью модуль/процедуру/функцию? Не повлияет это на код пользователя?

google translate:
Thank you for the information!  :) About "push" and "pop" I should have guessed... after all, in fact, the state could not be included initially.

The question is different, if I include it in the procedure block or in general for the whole module - will it work? Won't the state be immutable (if I disabled it) when "Exit" is called? Or will it return to its original state?

More precisely, do I need to separate certain commands or can I separate the module / procedure / function completely? Will this affect the user code?
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: StrToInt in pure pascal.
« Reply #42 on: January 18, 2022, 08:46:51 pm »
Any code gets compiled AFTER {$R+} till it meets:
{$R-} or
end of this unit or
{$pop} with the proper push & R

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: StrToInt in pure pascal.
« Reply #43 on: January 18, 2022, 10:17:02 pm »
Всем спасибо!

Проблема с диапазонами решена. Потому можно использовать без опасения на выход с ошибкой. Файл выложен под той же ссылкой (в первоначальном посте так же выложен).

Как обычно, выкладывайте тесты, которые привели к неправильному поведению функций. ))) Буду исправлять, если таковые имеются.

google translate:
Thank you all!

The problem with ranges is solved. Therefore, you can use it without fear of an exit with an error. The file is posted under the same link (in the original post it is also posted).

As usual, post the tests that led to the incorrect behavior of the functions. ))) I will correct, if any.
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

Seenkao

  • Hero Member
  • *****
  • Posts: 546
    • New ZenGL.
Re: StrToInt in pure pascal.
« Reply #44 on: January 19, 2022, 01:59:10 pm »
Хотел узнать, полезно будет или нет реализовать дополнительные функции?
Данные функции будут ориентированы на определённый тип данных. А так же для функций с числами без знака можно объединить с функциями работающими с шестнадцатеричными, восьмеричными и двоичными числами.

google translate:
 I wanted to know if it would be useful or not to implement additional functions?
These functions will target a specific data type. And also for functions with unsigned numbers, it can be combined with functions working with hexadecimal, octal and binary numbers.

Code: Pascal  [Select][+][-]
  1. function StrToIShort(const Str: String): ShortInt;
  2. function StrToISmall(const Str: String): SmallInt;
  3. function StrToInt(const Str: String): Integer;
  4. function StrToInt64(const Str: String): Int64;
  5.  
  6. function StrToByte(const Str: String): Byte;
  7. function StrToWord(const Str: String): Word;
  8. function StrToLWord(const Str: String): LongWord;
  9. function StrToQWord(const Str: String): QWord;
Rus: Стремлюсь к созданию минимальных и достаточно быстрых приложений.

Eng: I strive to create applications that are minimal and reasonably fast.
Working on ZenGL

 

TinyPortal © 2005-2018