Thread overview
strings
May 19, 2005
rko
May 19, 2005
Ben Hinkle
May 19, 2005
rko
May 19, 2005
Thomas Kuehne
May 23, 2005
Jim H
May 23, 2005
Lars Ivar Igesund
May 23, 2005
Derek Parnell
May 24, 2005
Lars Ivar Igesund
May 24, 2005
Derek Parnell
May 19, 2005
Andrew Fedoniouk
May 19, 2005
hi,

how does one convert strings from 'c' strings to 'd' strings and viceversa?

char[] x;
//'C'
char y[128];

x = y and y = x?

thanx

rko


May 19, 2005
"rko" <rko_member@pathlink.com> wrote in message news:d6hgl9$19vg$1@digitaldaemon.com...
> hi,
>
> how does one convert strings from 'c' strings to 'd' strings and viceversa?
>
> char[] x;
> //'C'
> char y[128];
>
> x = y and y = x?
>
> thanx
>
> rko

I'll answer this in two ways:
1) to convert a D string to a C string call std.string.toStringz. To convert
a C string to a D string write y[0 .. strlen(y)].
2) To copy data from one array to another (eg between char arrays) use
  y[0 .. numel] = x[0 .. numel];
where numel is the number of elements to copy.

The reason to separate converting C/D strings and copying data is that the
conversions can share data with the original string and don't necessarily
copy data - since the conversion has to do with 0-termination and length
computation.
Back you your variables, if you have a C string stored in y then to get a D
string slice of it run y[0 .. strlen(y)]. If you have a D string in x and
you want to copy it to y and make it a C string then run
  int numel = x.length>127?127:x.length;
  y[0 .. numel] = x[0 .. numel];
  y[numel] = 0;


May 19, 2005
In article <d6i0hv$1nnp$1@digitaldaemon.com>, Ben Hinkle says...
>
>
>"rko" <rko_member@pathlink.com> wrote in message news:d6hgl9$19vg$1@digitaldaemon.com...
>> hi,
>>
>> how does one convert strings from 'c' strings to 'd' strings and viceversa?
>>
>> char[] x;
>> //'C'
>> char y[128];
>>
>> x = y and y = x?
>>
>> thanx
>>
>> rko
>
>I'll answer this in two ways:
>1) to convert a D string to a C string call std.string.toStringz. To convert
>a C string to a D string write y[0 .. strlen(y)].
>2) To copy data from one array to another (eg between char arrays) use
>  y[0 .. numel] = x[0 .. numel];
>where numel is the number of elements to copy.
>
>The reason to separate converting C/D strings and copying data is that the
>conversions can share data with the original string and don't necessarily
>copy data - since the conversion has to do with 0-termination and length
>computation.
>Back you your variables, if you have a C string stored in y then to get a D
>string slice of it run y[0 .. strlen(y)]. If you have a D string in x and
>you want to copy it to y and make it a C string then run
>  int numel = x.length>127?127:x.length;
>  y[0 .. numel] = x[0 .. numel];
>  y[numel] = 0;
>
>

thanx a million. i know i am a nuisance but how does this work?

char[] a; // will get somewhere the content "test string"

now i would like to compare such as

if(a[0..3] == "test") { ....

and that does not work.


rko


May 19, 2005
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

rko schrieb am Thu, 19 May 2005 12:40:46 +0000 (UTC):
> thanx a million. i know i am a nuisance but how does this work?
>
> char[] a; // will get somewhere the content "test string"
>
> now i would like to compare such as
>
> if(a[0..3] == "test") { ....

	if(a[0..4] == "test")



-----BEGIN PGP SIGNATURE-----

iD8DBQFCjKZC3w+/yD4P9tIRApSbAKDExZY5iix4zRWHPkmqmkAcTW0KHACfYMVS
kNx/e4o77VPTqvnaDZdB59o=
=AoDi
-----END PGP SIGNATURE-----
May 19, 2005
"rko" <rko_member@pathlink.com> wrote in message news:d6hgl9$19vg$1@digitaldaemon.com...
> hi,
>
> how does one convert strings from 'c' strings to 'd' strings and viceversa?
>

Probably this "zoo" (below) from Harmonia will also help you:

module harmomia.utils.string;

import std.string;
import std.utf;

char[]  w2a( wchar[] w )  { return toUTF8(w); }
char[]  w2az( wchar[] w ) { return zstr!(char)(w2a(w)); }
char[]  wz2a( wchar* w )  { return toUTF8( wz2w(w) ); }
char[]  wz2az( wchar* w ) { return zstr!(char)(wz2a(w)); }

wchar[] a2w( char[] a )  { return toUTF16(a); }
wchar[] a2wz( char[] a ) { return zstr!(wchar)(a2w(a)); }
wchar[] az2w( char* a )  { return toUTF16( az2a(a) ); }
wchar[] az2wz( char* a ) { return zstr!(wchar)(az2w(a)); }

wchar[] wz2w( wchar* w ) { return w? w[0..wcslen(w)]: null; }
char[]    az2a( char* a ) { return a? a[0..strlen(a)]: null; }

wchar[] w2wz( wchar[] w ) { return zstr!(wchar)(w); }
char[]    a2az( char[] a )   { return zstr!(char)(a); }

template zstr(CHAR) // in-place zero terminate
{
  CHAR[] zstr(CHAR[] chars) // returns zero terminated string
  {
    uint l = chars.length;
    chars.length = l + 1;
    chars[l] = 0;
    chars.length = l;
    return chars;
  }
}




May 23, 2005
"Thomas Kuehne" <thomas-dloop@kuehne.this-is.spam.cn> wrote in message news:d8p9i2.496.thomas-dloop@laermschleuder.kuehne.cn...
>>
>> if(a[0..3] == "test") { ....
>
> if(a[0..4] == "test")
>

Yes, as a new user, I thought it was a bit odd that the second index of a slicing index was non-inclusive. It feels like you're indexing off the end of the array. As a C/C++ programmer, I'm fine with thinking in terms of (length-1) for zero-based indexing. It seemed to me that something like a[2..2] should return a one-element array consisting of index 2 rather than an empty array. But I suppose there must be a good rationale for it.

Jim



May 23, 2005
Jim H wrote:

> 
> "Thomas Kuehne" <thomas-dloop@kuehne.this-is.spam.cn> wrote in message news:d8p9i2.496.thomas-dloop@laermschleuder.kuehne.cn...
>>>
>>> if(a[0..3] == "test") { ....
>>
>> if(a[0..4] == "test")
>>
> 
> Yes, as a new user, I thought it was a bit odd that the second index of a slicing index was non-inclusive. It feels like you're indexing off the end of the array. As a C/C++ programmer, I'm fine with thinking in terms of (length-1) for zero-based indexing. It seemed to me that something like a[2..2] should return a one-element array consisting of index 2 rather than an empty array. But I suppose there must be a good rationale for it.
> 
> Jim

Because you need to be able to specify zero-length slices. If you have an array 'things', where you want to remove element 'ẍ́', where 0 > x > things.length, you can do this:

things = things[0..x] ~ things[(x + 1)..things.length];

If [0..0] were the first element, you wouldn't be able to remove it by setting x = 0;

Lars Ivar Igesund
May 23, 2005
On Mon, 23 May 2005 22:32:08 +0200, Lars Ivar Igesund wrote:

[snip]

> If [0..0] were the first element, you wouldn't be able to remove it by setting x = 0;

Just being a bit pedantic, but this only applies to 0-based indexing.

For languages that use 1-based indexing, they signal a zero length slice by using the form [x ..x-1], thus in those languages, [1..1] is a one-element slice and [1..0] is a zero length slice.

-- 
Derek Parnell
Melbourne, Australia
24/05/2005 7:46:54 AM
May 24, 2005
Derek Parnell wrote:

> On Mon, 23 May 2005 22:32:08 +0200, Lars Ivar Igesund wrote:
> 
> [snip]
> 
>> If [0..0] were the first element, you wouldn't be able to remove it by setting x = 0;
> 
> Just being a bit pedantic, but this only applies to 0-based indexing.

Yes, of course, but we are learning D, here, aren't we? :)
May 24, 2005
On Tue, 24 May 2005 21:09:21 +0200, Lars Ivar Igesund wrote:

> Derek Parnell wrote:
> 
>> On Mon, 23 May 2005 22:32:08 +0200, Lars Ivar Igesund wrote:
>> 
>> [snip]
>> 
>>> If [0..0] were the first element, you wouldn't be able to remove it by setting x = 0;
>> 
>> Just being a bit pedantic, but this only applies to 0-based indexing.
> 
> Yes, of course, but we are learning D, here, aren't we? :)

Oh yeah, that's right. I keep forgetting even the simplest of things. ;-)

-- 
Derek Parnell
Melbourne, Australia
25/05/2005 7:28:48 AM