Forum > Third party

Alternative set of string-to-int conversion routines

<< < (2/16) > >>

avk:
I just compiled this example:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program project1;{$MODE OBJFPC}{$H+}uses  SysUtils, Str2IntAlter; procedure Test(const s: string);var  I: Integer;begin  if TryChars2Int(s[22..Length(s)], I) then    WriteLn('I = ', I)  else    WriteLn('Ooooppss!');end; var  s: string = '';begin  Randomize;  s := 'it is random integer:  ' + Random(MaxInt).ToString;  Test(s);  ReadLn;end. 
To call TryChars2Int(s[22..Length(s)], I) FPC generated the following code:
32 bits

--- Code: ASM  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---# [10] if TryChars2Int(s[22..Length(s)], I) then        movl    %eax,%edx        testl   %eax,%eax        je      .Lj5        movl    -4(%edx),%edx.Lj5:        subl    $22,%edx        movl    %esp,%ecx        addl    $21,%eax        call    STR2INTALTER_$$_TRYCHARS2INT$array_of_CHAR$LONGINT$$BOOLEAN        testb   %al,%al        je      .Lj7# [11] WriteLn('I = ', I) 64 bits

--- Code: ASM  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---# [10] if TryChars2Int(s[22..Length(s)], I) then        movq    %rcx,%rdx# Peephole Optimization: %rdx = %rcx; changed to minimise pipeline stall (MovXXX2MovXXX)        testq   %rcx,%rcx        je      .Lj5        movq    -8(%rdx),%rdx.Lj5:        subq    $22,%rdx        leaq    32(%rsp),%r8        leaq    21(%rax),%rcx        call    STR2INTALTER_$$_TRYCHARS2INT$array_of_CHAR$LONGINT$$BOOLEAN        testb   %al,%al        je      .Lj7# [11] WriteLn('I = ', I) 
There doesn't seem to be any unnecessary copying going on.

zamtmn:
Oh yes, I completely forgot that

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---TryChars2Int(const a: array of char; ...this is not equivalent to

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---type  TTest=array of char;procedure TryChars2Int(const a:TTest; ...the request is closed :-[

Seenkao:
Благодарю за выложенный код!
Я буду знать, что я иду в правильном направлении!  :)

google translate:
Thanks for posting the code!
I will know that I am going in the right direction! :)

avk:
As per Thaddy's remarks, tried to somehow reduce code duplication.
Generic functions need to be declared in the interface part of the unit, and since they are helpers, this is not very good.
Includes are also not suitable, because I want everything to stay in one unit.
As a result, as an experiment, I settled on macros. The code looks unreadable, of course, but it seems that it would not be much better with includes.

zamtmn:
It became very ugly. In my opinion, the best solution would still be genetics and two modules:
Str2IntAlter - for uses in main unit
Str2IntAlterInternal - for uses in Str2IntAlter, contains generic help functions

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version