Author Topic: Intrinsic preprocessor string support enhancements  (Read 5058 times)

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Intrinsic preprocessor string support enhancements
« on: December 30, 2010, 02:09:57 AM »
As a heavy preprocessor user, I would definitely like to see the addition of the following preprocessor additions.
Note that the syntax is in keeping with the current nasm preprocessor syntax:

%strchr  %%pos 'StringToSearch', 'x'            ; %%pos assigned 0
%strstr  %%pos  'StringToSearch', 'To'          ; %% pos assigned 7

as well as their case-insenstive counterparts:

%strchri  %%pos 'StringToSearch', 'X'
%strstri  %%pos  'StringtoSearch', 'TO'

Also nice to haves would be ctype.h is* functions - eg:

%isalpha %%bool 'X'       ; %%bool assigned 1
%isdigit %%bool 'X'         ; %%bool assigned 0
%isalnum %%bool 'X'
%ispunc %%bool 'X'
%isspace %%bool 'X'

where 'X' can be any preprocessor defined string variable or literal

Oh, and a regex command would be incredible  ;D
« Last Edit: December 30, 2010, 02:12:45 AM by Rob Neff »

Offline Bryant Keller

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 360
  • Country: us
    • About Bryant Keller
Re: Intrinsic preprocessor string support enhancements
« Reply #1 on: December 30, 2010, 08:50:19 AM »
why not create macros to handle these specific tasks.. for example (strtok)

Code: (strtok.c) [Select]
%macro strtok 3
%push
%assign %$pos 1
%strlen %%len %{2}
%strlen %%span %{3}
%rep %%len
%substr %$ch %{2} %$pos, %%span
%ifidni %$ch,%{3}
%exitrep
%endif
%assign %$pos %$pos + 1
%endrep
%undef __STRCHR__
%define __STRCHR__ %[%$pos]
%pop
%assign %{1} __STRCHR__
%endmacro

%push
strtok %$x, "Hello, World!", ','
strtok %$y, "Hello, World!", "lo"
%warning %$x %$y
%pop

Case insensitive versions would be a little harder (you'd need to use %deftok and %defstr to try and "rebuild" the string arguments into uppercase form, then just recall the original case sensitive version... As for the %isXXX, those could also be really big %ifidni statements (%ifnum is already supported) wrapped in a macro also.

As for a regex command, one of my earlier experiments before doing NASMX was to try and write an EXPR macro which would simplify algebraic expressions and generate FPU math instructions. At the time it wasn't possible because as I tokenized the strings I couldn't rebuild symbols that had been broken up (they would have each character quoted in the new symbol).. This would "theoretically" be possible now since %deftok is available. I just haven't had the time to arse with trying it. That being said, you should be able to build a regex parser using the preprocessor itself (or even a scripting language if you get really froggy).
« Last Edit: December 30, 2010, 08:52:12 AM by Bryant Keller »

About Bryant Keller
bkeller@about.me

Offline Rob Neff

  • Forum Moderator
  • Full Member
  • *****
  • Posts: 430
  • Country: us
Re: Intrinsic preprocessor string support enhancements
« Reply #2 on: December 30, 2010, 05:03:40 PM »
why not create macros to handle these specific tasks.. for example (strtok)
I have a slew of macros slowly finding their way into nasmx.  For example, the strtok macro has been in nasmx.inc for a while now.  I would simply like to see a few more commands added as intrinsics to nasm similar to the ones already available (eg: substr, isnum, strlen). Not having those requires every nasm user to roll their own.  No big deal for me but how many various incantations of that functionality exist and how many have similarly named macros (think name space collision at the macro level - yes I'm only slightly paranoid  ;)
And from a performance perspective intrinsic commands should always outperform macros.

That being said, you should be able to build a regex parser using the preprocessor itself (or even a scripting language if you get really froggy).
I feel like I'm heading down that path already.  I'm sure the amount of macros you've written for oop probably makes you feel the same...