www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Phobos std.string lib should have a "char toupper( char ) & char tolower( char )"

reply David L. Davis <David_member pathlink.com> writes:
I'm not sure about anyone else, but strings have always been the "Meat and
Potato" of most of my programming career. And recently I've been playing around
with D type strings (Array of Characters...I know) and found that the toupper()
and tolower() functions in the std.string supports only the char[]'s with
multiple characters, but oddly enough not a char[] with a single character
(which it changes from a char[] to char when passed into the above functions.
Which would be kool, as long as there were some additional functions of
toupper() and tolower() that supported the following: char toupper( char ) and
char tolower( char ). 

Yeah, I know it's a small thing to many of you, but I do feel it should be added
into the std.string library of functions before D v1.0. 

Also why are the std.string const variable names in lowercase, I thought the
standard C/C++ way was to make all const variable names UPPERCASE. That way
there are easier to see in the code. Anyways, I just alias them from lowercase
version to uppercase within my code, just thought I'd point it out. :))

Below I wrote an example of the functions that would work with ascii characters.


import std.string;

char toupper
(
in char cChar
)    
{ 
// std.string const lowercase should be defined in all caps.
alias lowercase LOWERCASE;

if ( find( LOWERCASE, cChar ) != -1 )
// lowercase ascii a = '\x61', uppercase ascii A = '\x41'
return cChar - 0x20;
else
return cChar;
// end-if

} // end-function toupper

char tolower
(
in char cChar
)    
{ 
// std.string const lowercase should be defined in all caps.
alias uppercase UPPERCASE;

if ( find( UPPERCASE, cChar )  != -1 )
// lowercase ascii a = '\x61', uppercase ascii A = '\x41'
return cChar + 0x20;
else
return cChar;
// end-if

} // end-function tolower
May 25 2004
next sibling parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
"David L. Davis" <David_member pathlink.com> wrote in message
news:c8vq6c$1bd2$1 digitaldaemon.com...
 I'm not sure about anyone else, but strings have always been the "Meat and
 Potato" of most of my programming career. And recently I've been playing

 with D type strings (Array of Characters...I know) and found that the

 and tolower() functions in the std.string supports only the char[]'s with
 multiple characters, but oddly enough not a char[] with a single character
 (which it changes from a char[] to char when passed into the above

 Which would be kool, as long as there were some additional functions of
 toupper() and tolower() that supported the following: char toupper( char )

 char tolower( char ).

 Yeah, I know it's a small thing to many of you, but I do feel it should be

 into the std.string library of functions before D v1.0.

It's been in std.ctype for a long time; recently changed from char to dchar but still works with char.
May 25 2004
parent reply David L. Davis <David_member pathlink.com> writes:
In article <c8vrf7$1dcs$1 digitaldaemon.com>, Vathix says...
"David L. Davis" <David_member pathlink.com> wrote in message
news:c8vq6c$1bd2$1 digitaldaemon.com...
 I'm not sure about anyone else, but strings have always been the "Meat and
 Potato" of most of my programming career. And recently I've been playing

 with D type strings (Array of Characters...I know) and found that the

 and tolower() functions in the std.string supports only the char[]'s with
 multiple characters, but oddly enough not a char[] with a single character
 (which it changes from a char[] to char when passed into the above

 Which would be kool, as long as there were some additional functions of
 toupper() and tolower() that supported the following: char toupper( char )

 char tolower( char ).

 Yeah, I know it's a small thing to many of you, but I do feel it should be

 into the std.string library of functions before D v1.0.

It's been in std.ctype for a long time; recently changed from char to dchar but still works with char.

Vathix thanks for the reply. Ok that does work, tho it seems a bit weird that I have import another library ( std.ctype ) and have to use a cast( dchar ) to make it work. Shouldn't std.string still have a char version of both toupper() and tolower() to support the "D string-type" that has only one character, which then becomes a char when passed into case functions? import std.ctype; sStr[ iStrPos ] = toupper( cast( dchar )sStr[ iStrPos ] );
May 25 2004
parent reply "Vathix" <vathixSpamFix dprogramming.com> writes:
 Ok that does work, tho it seems a bit weird that I have import another

 std.ctype ) and have to use a cast( dchar ) to make it work. Shouldn't
 std.string still have a char version of both toupper() and tolower() to

 the "D string-type" that has only one character, which then becomes a char

 passed into case functions?

 import std.ctype;
 sStr[ iStrPos ] = toupper( cast( dchar )sStr[ iStrPos ] );

I think you're confused. provided char[] foo = "hello world"; foo[0] does not yield a string, it is only a single char of the char[]. If you want a string with one index then you'll need to slice: foo[0 .. 1] is a string and will work with std.string.toupper(). std.ctype works on single characters, which should not need to be cast to dchar; it is implicitly converted for me.
May 25 2004
parent David L. Davis <David_member pathlink.com> writes:
In article <c8vvig$1jvf$1 digitaldaemon.com>, Vathix says...
 Ok that does work, tho it seems a bit weird that I have import another

 std.ctype ) and have to use a cast( dchar ) to make it work. Shouldn't
 std.string still have a char version of both toupper() and tolower() to

 the "D string-type" that has only one character, which then becomes a char

 passed into case functions?

 import std.ctype;
 sStr[ iStrPos ] = toupper( cast( dchar )sStr[ iStrPos ] );

I think you're confused. provided char[] foo = "hello world"; foo[0] does not yield a string, it is only a single char of the char[]. If you want a string with one index then you'll need to slice: foo[0 .. 1] is a string and will work with std.string.toupper(). std.ctype works on single characters, which should not need to be cast to dchar; it is implicitly converted for me.

Vathix, "Thank you again very much"...you've been really helpful! :)) The below code changes how work as I wanted them to: //Equal to VB6 code written as: //Mid$(sStr, iStrPos, 1) = UCase$(Mid$(sStr, iStrPos, 1)) import std.string; sStr[ iStrPos .. iStrPos + 1 ] = toupper( sStr[ iStrPos .. iStrPos + 1 ] );
May 25 2004
prev sibling parent reply Andrew <Andrew_member pathlink.com> writes:
In article <c8vq6c$1bd2$1 digitaldaemon.com>, David L. Davis says...
I'm not sure about anyone else, but strings have always been the "Meat and
Potato" of most of my programming career. And recently I've been playing around
with D type strings (Array of Characters...I know) and found that the toupper()
and tolower() functions in the std.string supports only the char[]'s with
multiple characters, but oddly enough not a char[] with a single character
(which it changes from a char[] to char when passed into the above functions.
Which would be kool, as long as there were some additional functions of
toupper() and tolower() that supported the following: char toupper( char ) and
char tolower( char ). 

Yeah, I know it's a small thing to many of you, but I do feel it should be added
into the std.string library of functions before D v1.0. 

Also why are the std.string const variable names in lowercase, I thought the
standard C/C++ way was to make all const variable names UPPERCASE. That way
there are easier to see in the code. Anyways, I just alias them from lowercase
version to uppercase within my code, just thought I'd point it out. :))

Below I wrote an example of the functions that would work with ascii characters.


import std.string;

char toupper
(
in char cChar
)    
{ 
// std.string const lowercase should be defined in all caps.
alias lowercase LOWERCASE;

if ( find( LOWERCASE, cChar ) != -1 )
// lowercase ascii a = '\x61', uppercase ascii A = '\x41'
return cChar - 0x20;
else
return cChar;
// end-if

} // end-function toupper

char tolower
(
in char cChar
)    
{ 
// std.string const lowercase should be defined in all caps.
alias uppercase UPPERCASE;

if ( find( UPPERCASE, cChar )  != -1 )
// lowercase ascii a = '\x61', uppercase ascii A = '\x41'
return cChar + 0x20;
else
return cChar;
// end-if

} // end-function tolower

Not necessary: Functions are already established that addresses all capitalizeation needs with D strings (aka char[]). As far as I'm concerned, the only thing that needs to be modified is the capwords() function: which needs to take into consideration both uppercase and lowercase strings (it does not properly deal with uppercase strings). // import std.string; import std.stream; void main() { char[] string = "Demonstrates THE D string upper/lowercase functions"; // Output original string stdout.writeLine("original: " ~ string ~ "\n"); // Convert string to lowercase string = tolower(string); stdout.writeLine("tolower: " ~ string ~ "\n"); // Capitilize the first character of a given string string = capitalize(string); stdout.writeLine("capitalize: " ~ string ~ "\n"); // Capitalize the entire string string = toupper(string); stdout.writeLine("toupper: " ~ string ~ "\n"); // Capitalize the first letter of each word in a string string = capwords(tolower(string)); // needs to be fixed stdout.writeLine("capwords: " ~ string ~ "\n"); }
May 25 2004
parent David L. Davis <David_member pathlink.com> writes:
In article <c8vtai$1gbu$1 digitaldaemon.com>, Andrew says...
Not necessary:

Functions are already established that addresses all capitalizeation needs with
D strings (aka char[]). As far as I'm concerned, the only thing that needs to be
modified is the capwords() function: which needs to take into consideration both
uppercase and lowercase strings (it does not properly deal with uppercase
strings).

//
import std.string;
import std.stream;

void main()
{
char[] string = "Demonstrates THE D string upper/lowercase functions";

// Output original string
stdout.writeLine("original:   " ~ string ~ "\n");

// Convert string to lowercase
string = tolower(string);
stdout.writeLine("tolower:    " ~ string ~ "\n");

// Capitilize the first character of a given string
string = capitalize(string);
stdout.writeLine("capitalize: " ~ string ~ "\n");

// Capitalize the entire string
string =  toupper(string);
stdout.writeLine("toupper:    " ~ string ~ "\n");

// Capitalize the first letter of each word in a string
string = capwords(tolower(string)); // needs to be fixed
stdout.writeLine("capwords:   " ~ string ~ "\n");
}

capitalize the first character of each word. :) I look forward to it being fixed.
May 25 2004