Return the first character value of the specified string

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Return the first character value of the specified string

Post by IdeasVacuum »

Return the first character value of the specified string.

Currently we have the Asc() function to get the character number. In compiler ASCII mode, the result is the ASCII number, in compiler Unicode mode, the result is the UTF8 number.

However, the function name Asc() is very specific, the name suggests ASCII all the way. I would like to see a separate function to output Unicode character numbers, say: UTF8().

Each function to have dedicated type output regardless of compiler mode.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: Return the first character value of the specified string

Post by langinagel »

Maybe a solution:

Code: Select all

CompilerIf #PB_Compiler_Unicode
        Macro UTF(a.s)
                Asc(a.s)
        EndMacro
CompilerEndIf
But surely as helpful as any other macro-solution.

Greetings
LN
https://www.doerpsoft.org

Boost. Work. Efficiency.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Return the first character value of the specified string

Post by IdeasVacuum »

A macro is a good work around. Your macro though ties the compilation to Unicode, which is not wanted. It is possible for an ASCII compiled program to read a Unicode file.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Return the first character value of the specified string

Post by skywalk »

The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Return the first character value of the specified string

Post by IdeasVacuum »

Read the first post! The answer is Asc(), but that function name does not suggest Unicode.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
skywalk
Addict
Addict
Posts: 4211
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Return the first character value of the specified string

Post by skywalk »

Haha, I thought you said Asc() is not the answer since it is #PB_Compiler_Unicode dependent?
PeekU() works the same in Ascii/Unicode compile.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Return the first character value of the specified string

Post by Fred »

Asc() already returns the unicode character value (not UTF8, as PB doesn't handle UTF8 in native) in unicode. I don't really undestand the question as well :)
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Return the first character value of the specified string

Post by IdeasVacuum »

Well:
the function name Asc() is very specific, the name suggests ASCII all the way.
When you have that function in your code, you don't immediately know if it is returning an ASCII number or a Unicode number........
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Return the first character value of the specified string

Post by Fred »

True. Anyway the function was here to return the first character value, so when we introduced unicode support, it was more or less obvious it would return the first unicode character value, despite of the name.
Zach
Addict
Addict
Posts: 1675
Joined: Sun Dec 12, 2010 12:36 am
Location: Somewhere in the midwest
Contact:

Re: Return the first character value of the specified string

Post by Zach »

Maybe Fred should just rename the command to something non-offensive :mrgreen:
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Return the first character value of the specified string

Post by PB »

> I would like to see a separate function to output Unicode character numbers

So, should every string command have two versions now? One for Ascii and
one for Unicode return values? I don't think that's a good idea to implement.
Perhaps an optional flag can be added for existing commands, but keep them
defaulting to Ascii if the flag is omitted. I don't know. Where is the line drawn?
I compile using 5.31 (x86) on Win 7 Ultimate (64-bit).
"PureBasic won't be object oriented, period" - Fred.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Return the first character value of the specified string

Post by IdeasVacuum »

Where is the line drawn?
Ah, underlining text, that's another subject :mrgreen:
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
langinagel
Enthusiast
Enthusiast
Posts: 131
Joined: Fri Jan 28, 2005 11:53 pm
Location: Germany
Contact:

Re: Return the first character value of the specified string

Post by langinagel »

I second PBs point.
Let's have one ASC() function that provides the correct result in each mode, ASCII or UTF().
The compiler shall take care of this.
ASC() is definitely BASIC standard function, thus widely understandable.
https://www.doerpsoft.org

Boost. Work. Efficiency.
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Return the first character value of the specified string

Post by IdeasVacuum »

thus widely understandable
...which is my point. The result could easily be assumed to be ASCII when its Unicode.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: Return the first character value of the specified string

Post by Danilo »

IdeasVacuum wrote:The result could easily be assumed to be ASCII when its Unicode.
The behavior of Asc() is documented. The name of the function comes from a time
when there was no Unicode (also in PB). VB.NET has functions Asc() and AscW(), in PB it is Asc() for both modes.

If I understand correctly, you just would like a name change, like:

Code: Select all

Macro FirstCharacter(string)
    Asc(string)
EndMacro

Debug FirstCharacter("Hello")
:?:
Post Reply