News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

Fast char type evaluation

Started by hutch--, February 21, 2022, 01:47:23 PM

Previous topic - Next topic

hutch--

For people who need to work on single character recognition, the "char_type" algo is reasonably fast, its main overhead is the default basic function entry and exit. It can be done faster with a FASTPROC but that makes it harder to use. It is designed to evaluate single characters and will only read the first character of a multi-character string passed to it.

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

    #compile sll

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

FUNCTION char_type(char$) COMMON as DWORD
  ' -------------------------------------------------------
  ' algorithm will only read the first character in "char$"
  ' -------------------------------------------------------
    LOCAL pchar as DWORD

    ch1$  = left$(char$,1)                  ' get the left character
    pchar = StrPtr(ch1$)                    ' get its address

  PREFIX "!"
    mov eax, pchar                          ; load character address
    mov eax, [eax]                          ; dereference eax
    lea ecx, chtable                        ; load table address
    movzx edx, BYTE PTR [ecx+eax]           ; zero extend table + char offset
    mov FUNCTION, edx                       ; return the character type
    jmp bye

  ; --------------------------------
  ; English language character table
  ; --------------------------------
  chtable:
    db 6,6,6,6,6,6,6,6,6,8,0,6,6,9,6,6
    db 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6
    db 7,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5
    db 1,1,1,1,1,1,1,1,1,1,5,5,5,5,5,5
    db 5,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2
    db 2,2,2,2,2,2,2,2,2,2,2,5,5,5,5,5
    db 5,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3
    db 3,3,3,3,3,3,3,3,3,3,3,5,5,5,5,5
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
    db 4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4
  END PREFIX

    ' 1 = numbers
    ' 2 = upper case
    ' 3 = lower case
    ' 4 = high ascii/ansi
    ' 5 = punctuation
    ' 6 = control characters
    ' 7 = space
    ' 8 = tab
    ' 9 = carriage return
    ' 0 = line feed

  bye:

End FUNCTION

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