Re: string to char conv

2018-08-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, August 13, 2018 6:42:02 PM MDT zeus via Digitalmars-d-learn 
wrote:
> On Tuesday, 14 August 2018 at 00:24:53 UTC, Jonathan M Davis
>
> wrote:
> > On Monday, August 13, 2018 6:06:22 PM MDT zeus via
> >
> > Digitalmars-d-learn wrote:
> >> [...]
> >
> > Why are you casting the string to a char*? That's just going to
> > make writeln print out the pointer value. If you want to print
> > out the value of the string, then just pass the string to
> > writeln.
> >
> > - Jonathan M Davis
>
> Needed char* for while (isspace(*testi)) --testi; etc

Why would you be calling C functions for basic stuff like that? Just use
std.ascii.isWhite (or std.uni.isWhite if you want Unicode whitespace) if you
want to check whether a character is whitespace, and there's really no
reason to use pointers with D strings. But if you just want to strip the
whitespace off of the front of a string, then use std.string.stripLeft. e.g.

auto result = str.stripLeft();

Or a more general solution would involve using std.algorithm.searching.find
to find a specific character, or a character which matches a predicate.
stripLeft is basically a more efficient version of

auto result = str.find!(a => !isWhite(a))();

Very little D code is ever going to be doing anything with char* unless it's
calling toStringz on a string to pass it to a C function, because there is
no D equivalent. Using char* loses the length of the string and loses out on
bounds-checking. And since D strings are not zero-terminated, it usually
does not work well at all to operate on a char* as if it were a string.

- Jonathan M Davis





Re: string to char conv

2018-08-13 Thread zeus via Digitalmars-d-learn
On Tuesday, 14 August 2018 at 00:24:53 UTC, Jonathan M Davis 
wrote:
On Monday, August 13, 2018 6:06:22 PM MDT zeus via 
Digitalmars-d-learn wrote:

[...]


Why are you casting the string to a char*? That's just going to 
make writeln print out the pointer value. If you want to print 
out the value of the string, then just pass the string to 
writeln.


- Jonathan M Davis


Needed char* for while (isspace(*testi)) --testi; etc


Re: string to char conv

2018-08-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, August 13, 2018 6:24:53 PM MDT Jonathan M Davis via Digitalmars-
d-learn wrote:
> On Monday, August 13, 2018 6:06:22 PM MDT zeus via Digitalmars-d-learn
>
> wrote:
> > i have the following code in d and i get as result 4D77EB, also i
> > have the following code in c++ wich give me as results
> > 0xABCDEF123abcdef12345678909832190 how i can get in d
> > 0xABCDEF123abcdef12345678909832190 instead of 4D77EB
> >
> >
> > // D
> >
> > void test(string test){
> >
> >   char* testi = cast(char*)(test);
> >   writeln(testi);
> >
> > }
> >
> >
> > void main()
> > {
> >
> >   test("0xABCDEF123abcdef12345678909832190");
> >
> > }
> >
> >
> > // C++
> >
> > void test(string str){
> >
> >  const char* testi = str.c_str();
> >  printf("%s\n", testi);
> >
> > }
> >
> > int main(int argc, char const *argv[]){
> >
> >  test("0xABCDEF123abcdef12345678909832190");
> >
> > }
>
> Why are you casting the string to a char*? That's just going to make
> writeln print out the pointer value. If you want to print out the value
> of the string, then just pass the string to writeln.

I suppose that I should point out that you almost never want to use char* in
D unless interacting with C or C++. Strings in D are not zero-terminated.
They are dynamic arrays of characters. String literals do have a '\0' one
past their end so that you can pass string literals to C functions which
take const char*, and std.string.toStringz can be used to convert a string
to a zero-terminated immutable(char)*, but in general, you just don't use
zero-terminated strings in D, and writeln is far more flexible than printf,
since it will accept pretty much any type. I'd suggest that you read the
documentation for the writeln family of functions:

https://dlang.org/phobos/std_stdio.html#.writeln
https://dlang.org/phobos/std_stdio.html#.writefln
https://dlang.org/phobos/std_stdio.html#.write
https://dlang.org/phobos/std_stdio.html#.writef

and the documentation for formattedWrite explains the flags accepted by the
various functions that accept format strings (e.g. writefln or format):

https://dlang.org/phobos/std_format.html#.formattedWrite

- Jonathan M Davis





Re: string to char conv

2018-08-13 Thread Jonathan M Davis via Digitalmars-d-learn
On Monday, August 13, 2018 6:06:22 PM MDT zeus via Digitalmars-d-learn 
wrote:
> i have the following code in d and i get as result 4D77EB, also i
> have the following code in c++ wich give me as results
> 0xABCDEF123abcdef12345678909832190 how i can get in d
> 0xABCDEF123abcdef12345678909832190 instead of 4D77EB
>
>
> // D
>
> void test(string test){
>   char* testi = cast(char*)(test);
>   writeln(testi);
>
> }
>
>
> void main()
> {
>   test("0xABCDEF123abcdef12345678909832190");
> }
>
>
> // C++
>
> void test(string str){
>  const char* testi = str.c_str();
>  printf("%s\n", testi);
>
> }
>
> int main(int argc, char const *argv[]){
>
>  test("0xABCDEF123abcdef12345678909832190");
> }

Why are you casting the string to a char*? That's just going to make writeln
print out the pointer value. If you want to print out the value of the
string, then just pass the string to writeln.

- Jonathan M Davis





string to char conv

2018-08-13 Thread zeus via Digitalmars-d-learn
i have the following code in d and i get as result 4D77EB, also i 
have the following code in c++ wich give me as results 
0xABCDEF123abcdef12345678909832190 how i can get in d 
0xABCDEF123abcdef12345678909832190 instead of 4D77EB



// D

void test(string test){
char* testi = cast(char*)(test);
writeln(testi);

}


void main()
{
test("0xABCDEF123abcdef12345678909832190");
}


// C++

void test(string str){
const char* testi = str.c_str();
printf("%s\n", testi);

}

int main(int argc, char const *argv[]){

test("0xABCDEF123abcdef12345678909832190");
}