Announcement

Collapse
No announcement yet.

IsChar...

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • IsChar...

    Some useful API character functions. A tip to PB staff could be to add PB versions of these to the already powerful string handling toolbox..
    Code:
    [color=007F00]'IsCharAlpha
    '  Determines if a character is alphabetic, returns 1 if it is, else 0.
      [/color][color=0000C0]IF [/color][color=000000]IsCharAlpha[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"a"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlpha returns 1
      [/color][color=0000C0]IF [/color][color=000000]IsCharAlpha[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"9"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlpha returns 0
      [/color][color=0000C0]IF [/color][color=000000]IsCharAlpha[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"?"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlpha returns 0
    
    'IsCharAlphaNumeric
    '  Determines if a character is either an alphabetic or a numeric, returns 1 if it is, else 0.
      [/color][color=0000C0]IF [/color][color=000000]IsCharAlphaNumeric[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"a"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlphaNumeric returns 1
      [/color][color=0000C0]IF [/color][color=000000]IsCharAlphaNumeric[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"9"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlphaNumeric returns 1
      [/color][color=0000C0]IF [/color][color=000000]IsCharAlphaNumeric[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"?"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlphaNumeric returns 0
    
    'IsCharLower
    '  Determines if a character is lowercase, returns 1 if it is, else 0.
      [/color][color=0000C0]IF [/color][color=000000]IsCharLower[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"a"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharLower returns 1
      [/color][color=0000C0]IF [/color][color=000000]IsCharLower[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"A"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharLower returns 0
    
    'IsCharUpper
    '  Determines if a character is uppercase, returns 1 if it is, else 0.
      [/color][color=0000C0]IF [/color][color=000000]IsCharUpper[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"a"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharUpper returns 0
      [/color][color=0000C0]IF [/color][color=000000]IsCharUpper[/color][color=8000FF]([/color][color=0000C0]ASC[/color][color=8000FF]([/color][color=C020C0]"A"[/color][color=8000FF])) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharUpper returns 1
    
    ' All of these API functions can be very useful when working with pointers:
     [/color][color=0000C0]LOCAL [/color][color=000000]pChr [/color][color=0000C0]AS BYTE PTR[/color][color=8000FF], [/color][color=000000]sChar [/color][color=0000C0]AS STRING
     [/color][color=000000]sChar [/color][color=8000FF]= [/color][color=C020C0]"a"
     [/color][color=000000]pChr  [/color][color=8000FF]= [/color][color=0000C0]STRPTR[/color][color=8000FF]([/color][color=000000]sChar[/color][color=8000FF])
     [/color][color=0000C0]IF [/color][color=000000]IsCharAlpha[/color][color=8000FF](@[/color][color=000000]pChr[/color][color=8000FF])        [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlpha returns 1
     [/color][color=0000C0]IF [/color][color=000000]IsCharAlphaNumeric[/color][color=8000FF](@[/color][color=000000]pChr[/color][color=8000FF]) [/color][color=0000C0]THEN [/color][color=007F00]' IsCharAlphaNumeric returns 1
     [/color][color=0000C0]IF [/color][color=000000]IsCharLower[/color][color=8000FF](@[/color][color=000000]pChr[/color][color=8000FF])        [/color][color=0000C0]THEN [/color][color=007F00]' IsCharLower returns 1
     [/color][color=0000C0]IF [/color][color=000000]IsCharUpper[/color][color=8000FF](@[/color][color=000000]pChr[/color][color=8000FF])        [/color][color=0000C0]THEN [/color][color=007F00]' IsCharUpper returns 0
    [/color]

  • #2
    I can see where some might find those useful. Seems like it'll be a real bear to implement though. Characters vary by font, especially dicritic characters above 128 in eight bit; and vary some more by language. And wide characters would need a second set of functions, or 8 bit/16 bit detection in each function.

    Later
    Dale

    Comment


    • #3
      Dale, those APIs are available in both "A" and "W" versions.
      ==>
      Code:
      ----------------------------------------
      Find 'ischaralpha' in 'D:\Software_Development\PBWin10\WinAPI\WinUser.inc' (12/26/2011 2:22:26 PM; 12/1/2011 11:03:00 AM):
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4387): DECLARE FUNCTION IsCharAlphaA LIB "User32.dll" ALIAS "IsCharAlphaA" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4390): DECLARE FUNCTION IsCharAlphaW LIB "User32.dll" ALIAS "IsCharAlphaW" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4394): DECLARE FUNCTION IsCharAlpha LIB "User32.dll" ALIAS "IsCharAlphaW" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4397): DECLARE FUNCTION IsCharAlpha LIB "User32.dll" ALIAS "IsCharAlphaA" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4401): DECLARE FUNCTION IsCharAlphaNumericA LIB "User32.dll" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4402):     ALIAS "IsCharAlphaNumericA" (BYVAL cChar AS BYTE) AS LONG
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4404): DECLARE FUNCTION IsCharAlphaNumericW LIB "User32.dll" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4405):     ALIAS "IsCharAlphaNumericW" (BYVAL cChar AS WORD) AS LONG
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4408): DECLARE FUNCTION IsCharAlphaNumeric LIB "User32.dll" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4409):     ALIAS "IsCharAlphaNumericW" (BYVAL cChar AS WORD) AS LONG
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4411): DECLARE FUNCTION IsCharAlphaNumeric LIB "User32.dll" _
      D:\Software_Development\PBWin10\WinAPI\WinUser.inc(4412):     ALIAS "IsCharAlphaNumericA" (BYVAL cChar AS BYTE) AS LONG
      Found 'ischaralpha' 16 time(s).
      Search complete, found 'ischaralpha' 16 time(s). (1 file(s)).
      The "W" functions certainly make "Wide char in general" easier and someone who does international programming would know if maybe the diacritical characters are kind of "automatically" handled if you create your application as a Wide Char app ( PB/Win 10+, PB/CC 6+)

      (Ultra Edit, in case you are curious).


      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Okay, but if you would read for change, you'd see I wasn't strongly in favor of the effort.
        Last edited by Dale Yarker; 25 Apr 2016, 07:03 PM.
        Dale

        Comment


        • #5
          Those functions have never failed for me in Windows, using standard text fonts and they have been very useful for creating own "Toggle case" and look-up tables. Good thing is, they are localized, so code written here in Sweden also works in USA and France, etc. And vice versa, which is more important, because many european languages have more letters than english a-z. Had some interesting diskussion about this with Bob years ago, because early versions of the compiler only handled the US alphabet. He understood the problem and fixed that issue, good old Bob.
          .
          MS speaks: "This determination is based on the semantics of the language selected by the user during setup or by using Control Panel.".
          And yes, of course there are wide versions of all the calls too - thank you, Michael! Then use INTEGER pointer instead of byte and set %UNICODE = 1 before including Win32api.inc (PBWIN10/PBCC6).

          Example of simple toggle case - very useful when one has managed to press Caps Lock and has typed lots of text without looking. I actually have this in my own code editor, because some stupid programmer constantly manage press that key. Then makes it easy to select typed comments/text and toggle the characters to what they were supposed to be. Anyway, there's a speedy asm version of this too (AnsiTCASE, etc) in source code forum, if someone is in a hurry.. See http://forum.powerbasic.com/forum/us...n-7-0-or-later

          Code:
          [COLOR=007F00]'====================================================================
          ' Simple tOGGLE cASE in given text string.
          '--------------------------------------------------------------------[/COLOR]
          [COLOR=0000C0]FUNCTION [/COLOR][COLOR=000000]TCase[/COLOR][COLOR=8000FF]([/COLOR][COLOR=0000C0]BYVAL [/COLOR][COLOR=000000]sTemp [/COLOR][COLOR=0000C0]AS STRING[/COLOR][COLOR=8000FF]) [/COLOR][COLOR=0000C0]AS STRING[/COLOR]
          [COLOR=007F00]'--------------------------------------------------------------------
            [/COLOR][COLOR=0000C0]LOCAL [/COLOR][COLOR=000000]c [/COLOR][COLOR=0000C0]AS LONG[/COLOR][COLOR=8000FF], [/COLOR][COLOR=000000]pStr [/COLOR][COLOR=0000C0]AS BYTE PTR
          
            [/COLOR][COLOR=000000]pStr [/COLOR][COLOR=8000FF]= [/COLOR][COLOR=0000C0]STRPTR[/COLOR][COLOR=8000FF]([/COLOR][COLOR=000000]sTemp[/COLOR][COLOR=8000FF])
            [/COLOR][COLOR=0000C0]FOR [/COLOR][COLOR=000000]c [/COLOR][COLOR=8000FF]= [/COLOR][COLOR=000000]1 [/COLOR][COLOR=0000C0]TO LEN[/COLOR][COLOR=8000FF]([/COLOR][COLOR=000000]sTemp[/COLOR][COLOR=8000FF])
                [/COLOR][COLOR=0000C0]IF [/COLOR][COLOR=000000]IsCharUpper[/COLOR][COLOR=8000FF](@[/COLOR][COLOR=000000]pStr[/COLOR][COLOR=8000FF]) [/COLOR][COLOR=0000C0]THEN
                   [/COLOR][COLOR=000000]CharLowerBuff [/COLOR][COLOR=0000C0]BYVAL [/COLOR][COLOR=000000]pStr[/COLOR][COLOR=8000FF], [/COLOR][COLOR=000000]1
                [/COLOR][COLOR=0000C0]ELSEIF [/COLOR][COLOR=000000]IsCharLower[/COLOR][COLOR=8000FF](@[/COLOR][COLOR=000000]pStr[/COLOR][COLOR=8000FF]) [/COLOR][COLOR=0000C0]THEN
                   [/COLOR][COLOR=000000]CharUpperBuff [/COLOR][COLOR=0000C0]BYVAL [/COLOR][COLOR=000000]pStr[/COLOR][COLOR=8000FF], [/COLOR][COLOR=000000]1
                [/COLOR][COLOR=0000C0]END IF
                INCR [/COLOR][COLOR=000000]pStr
            [/COLOR][COLOR=0000C0]NEXT
          
            FUNCTION [/COLOR][COLOR=8000FF]= [/COLOR][COLOR=000000]sTemp[/COLOR]
          
          [COLOR=0000C0]END FUNCTION[/COLOR]
          Last edited by Borje Hagsten; 26 Apr 2016, 04:11 AM.

          Comment


          • #6
            Roll your own

            Code:
            $Lower = CHR$(97 TO 122)
            $Upper = CHR$(65 TO 90)
            $Numeric = CHR$(48 TO 57)
            $Alpha = $Lower + $Upper
            $AlphaNumeric = $Lower + $Upper + $Numeric
            MACRO isCharAlpha(parm1) = INSTR($Alpha,parm1)
            MACRO isCharAlphaNumeric(parm1) = INSTR($AlphaNumeric,parm1)
            MACRO isCharLower(parm1) = INSTR($Lower,parm1)
            MACRO isCharUpper(parm1) = INSTR($Upper,parm1)

            Comment


            • #7
              Yes Mike, but 97 to 122 only works with english alphabet. There are letters beyond z too and that's where the IsChar.. functions are extra useful

              Comment


              • #8
                What I don't understand is the wish to implement them in PB when they are already available.
                Forum: http://www.jose.it-berater.org/smfforum/index.php

                Comment


                • #9
                  > What I don't understand is the wish to implement them in PB when they are already available

                  So are UCASE and LCASE, etc. too, but we never can get enough of easy-to-use string handling tools that doesn't need Win32api.inc plus knowledge that they even exist and/or how to use them, can we?

                  Comment

                  Working...
                  X