The MASM Forum

Specialised Projects => PowerBASIC => Topic started by: hutch-- on January 01, 2020, 01:06:48 AM

Title: Zero backtrim algo.
Post by: hutch-- on January 01, 2020, 01:06:48 AM
Algorithm for back trimming trailing zeros in a basic string.

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

    #compile exe "backtrimz.exe"
    #compiler pbcc                          ' only build with PBCC
    #option largemem32                      ' /LARGEADDRESSAWARE

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

 FUNCTION PBmain as LONG

    a$ = chr$("1234567890",0,0,0,0,0,0,0,0)
    b$ = backtrimz(a$)
    StdOut b$
    StdOut format$(len(b$))

    waitkey$

 End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION backtrimz(src$) as STRING
  ' -------------------------------------
  ' trim trailing zeros from basic string
  ' -------------------------------------
    #REGISTER NONE

    LOCAL lstr as DWORD                     ' string length
    LOCAL pstr as DWORD                     ' string pointer

    lstr = len(src$)                        ' src$ length
    pstr = StrPtr(src$)                     ' src$ pointer

  PREFIX "!"

    mov esi, pstr                           ' load pointer address
    cmp BYTE PTR [esi], 0                   ' exit on leading zero
    je errr
    add esi, lstr                           ' add length to pointer address

  lbl:
    sub esi, 1                              ' decrement address
    cmp BYTE PTR [esi], 0                   ' test if char is 0
    je lbl                                  ' jump back on zero

    sub esi, pstr                           ' subtract pointer from esi to get new length
    mov lstr, esi                           ' copy esi to variable
    add lstr, 1                             ' correct for last sub

  END PREFIX

    FUNCTION = left$(src$,lstr)             ' return back trimmed BASIC string
    Exit FUNCTION

  errr:
    FUNCTION = ""                           ' exit with null string on error

 End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤