Page 1 of 1

Return the first character value of the specified string

Posted: Mon Mar 31, 2014 11:04 am
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.

Re: Return the first character value of the specified string

Posted: Wed May 07, 2014 8:55 pm
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

Re: Return the first character value of the specified string

Posted: Wed May 07, 2014 9:21 pm
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.

Re: Return the first character value of the specified string

Posted: Wed May 07, 2014 9:32 pm
by skywalk

Re: Return the first character value of the specified string

Posted: Wed May 07, 2014 10:25 pm
by IdeasVacuum
Read the first post! The answer is Asc(), but that function name does not suggest Unicode.

Re: Return the first character value of the specified string

Posted: Wed May 07, 2014 10:53 pm
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.

Re: Return the first character value of the specified string

Posted: Thu May 08, 2014 9:51 am
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 :)

Re: Return the first character value of the specified string

Posted: Thu May 08, 2014 10:39 am
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........

Re: Return the first character value of the specified string

Posted: Thu May 08, 2014 11:49 am
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.

Re: Return the first character value of the specified string

Posted: Thu May 08, 2014 5:10 pm
by Zach
Maybe Fred should just rename the command to something non-offensive :mrgreen:

Re: Return the first character value of the specified string

Posted: Fri May 09, 2014 8:40 am
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?

Re: Return the first character value of the specified string

Posted: Fri May 09, 2014 3:03 pm
by IdeasVacuum
Where is the line drawn?
Ah, underlining text, that's another subject :mrgreen:

Re: Return the first character value of the specified string

Posted: Mon May 12, 2014 6:10 pm
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.

Re: Return the first character value of the specified string

Posted: Mon May 12, 2014 6:43 pm
by IdeasVacuum
thus widely understandable
...which is my point. The result could easily be assumed to be ASCII when its Unicode.

Re: Return the first character value of the specified string

Posted: Mon May 12, 2014 7:34 pm
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")
:?: