Jump to page: 1 25  
Page
Thread overview
string to char array?
Jun 02, 2015
Kyoji Klyden
Jun 02, 2015
Dennis Ritchie
Jun 02, 2015
Kyoji Klyden
Jun 02, 2015
Meta
Jun 02, 2015
Alex Parrill
Jun 02, 2015
Dennis Ritchie
Jun 02, 2015
Kyoji Klyden
Jun 02, 2015
Alex Parrill
Jun 02, 2015
Kyoji Klyden
Jun 02, 2015
Alex Parrill
Jun 02, 2015
Kyoji Klyden
Jun 03, 2015
Kagamin
Jun 03, 2015
Kyoji Klyden
Jun 03, 2015
Marc Schütz
Jun 03, 2015
Kyoji Klyden
Jun 03, 2015
anonymous
Jun 03, 2015
Kyoji Klyden
Jun 03, 2015
anonymous
Jun 03, 2015
Kyoji Klyden
Jun 03, 2015
anonymous
Jun 03, 2015
Kagamin
Jun 03, 2015
Kyoji Klyden
Jun 03, 2015
Kagamin
Jun 04, 2015
ketmar
Jun 04, 2015
Kyoji Klyden
Jun 04, 2015
anonymous
Jun 04, 2015
Ali Çehreli
Jun 05, 2015
Kyoji Klyden
Jun 05, 2015
Kagamin
Jun 05, 2015
Kyoji Klyden
Jun 05, 2015
Kagamin
Jun 05, 2015
Kyoji Klyden
Jun 06, 2015
Marc Schütz
Jun 06, 2015
Kyoji Klyden
Jun 06, 2015
Marc Schütz
Jun 07, 2015
Kyoji Klyden
Jun 08, 2015
Kagamin
Jun 08, 2015
Kyoji Klyden
Jun 05, 2015
anonymous
Jun 05, 2015
Kyoji Klyden
Jun 05, 2015
anonymous
Jun 05, 2015
Kyoji Klyden
Jun 06, 2015
sigod
June 02, 2015
quick question: What is the most efficient way to covert a string to a char array?

June 02, 2015
On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
> quick question: What is the most efficient way to covert a string to a char array?

string s = "str";
char[] strArr = s.dup;
June 02, 2015
On Tuesday, 2 June 2015 at 15:26:50 UTC, Dennis Ritchie wrote:
> On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
>> quick question: What is the most efficient way to covert a string to a char array?
>
> string s = "str";
> char[] strArr = s.dup;

Thanks! :)
June 02, 2015
On Tuesday, 2 June 2015 at 15:32:12 UTC, Kyoji Klyden wrote:
> On Tuesday, 2 June 2015 at 15:26:50 UTC, Dennis Ritchie wrote:
>> On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
>>> quick question: What is the most efficient way to covert a string to a char array?
>>
>> string s = "str";
>> char[] strArr = s.dup;
>
> Thanks! :)

Note that this will allocate a new garbage collected array.
June 02, 2015
On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
> quick question: What is the most efficient way to covert a string to a char array?

A string is, by definition in D, a character array, specifically `immutable(char)[]`. It's not like, for example, Java in which it's a completely separate type; you can perform all the standard array operations on strings.

If you need to mutate a string, then you can create a mutable `char[]` by doing `somestring.dup` as Dennis already mentioned.
June 02, 2015
On Tuesday, 2 June 2015 at 15:53:33 UTC, Alex Parrill wrote:
> A string is, by definition in D, a character array, specifically `immutable(char)[]`. It's not like, for example, Java in which it's a completely separate type; you can perform all the standard array operations on strings.

Yes, I believe that this is a problem in D, and because when you create a multidimensional array mutable strings having real troubles with .deepDup. I think that will solve the problem of a new string data type is a built-in D, and because writing .dup, to create a mutated string, - it's really funny! This is problem!
June 02, 2015
On Tuesday, 2 June 2015 at 15:53:33 UTC, Alex Parrill wrote:
> On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
>> quick question: What is the most efficient way to covert a string to a char array?
>
> A string is, by definition in D, a character array, specifically `immutable(char)[]`. It's not like, for example, Java in which it's a completely separate type; you can perform all the standard array operations on strings.
>
> If you need to mutate a string, then you can create a mutable `char[]` by doing `somestring.dup` as Dennis already mentioned.

The problem I was having was actually that an opengl function (specifically glShaderSource) wouldn't accept strings. I'm still can't get it to work actually :P

glShaderSource (uint, int, const(char*)*, const(int)*)

This one function is a bugger, been going at this for hours.


On Tuesday, 2 June 2015 at 15:38:24 UTC, Meta wrote:
>
> Note that this will allocate a new garbage collected array.

Thx for the heads up
June 02, 2015
On Tuesday, 2 June 2015 at 16:23:26 UTC, Kyoji Klyden wrote:
> On Tuesday, 2 June 2015 at 15:53:33 UTC, Alex Parrill wrote:
>> On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
>>> quick question: What is the most efficient way to covert a string to a char array?
>>
>> A string is, by definition in D, a character array, specifically `immutable(char)[]`. It's not like, for example, Java in which it's a completely separate type; you can perform all the standard array operations on strings.
>>
>> If you need to mutate a string, then you can create a mutable `char[]` by doing `somestring.dup` as Dennis already mentioned.
>
> The problem I was having was actually that an opengl function (specifically glShaderSource) wouldn't accept strings. I'm still can't get it to work actually :P
>
> glShaderSource (uint, int, const(char*)*, const(int)*)
>
> This one function is a bugger, been going at this for hours.
>
>
> On Tuesday, 2 June 2015 at 15:38:24 UTC, Meta wrote:
>>
>> Note that this will allocate a new garbage collected array.
>
> Thx for the heads up

glShaderSource accepts an array of null-terminated strings. Try this:


	import std.string : toStringz;

	string sources = source.toStringz;
	int len = source.length;

	glShaderSource(id, sources, 1, &sources, &len);


June 02, 2015
On Tuesday, 2 June 2015 at 16:26:30 UTC, Alex Parrill wrote:
> On Tuesday, 2 June 2015 at 16:23:26 UTC, Kyoji Klyden wrote:
>> On Tuesday, 2 June 2015 at 15:53:33 UTC, Alex Parrill wrote:
>>> On Tuesday, 2 June 2015 at 15:07:58 UTC, Kyoji Klyden wrote:
>>>> quick question: What is the most efficient way to covert a string to a char array?
>>>
>>> A string is, by definition in D, a character array, specifically `immutable(char)[]`. It's not like, for example, Java in which it's a completely separate type; you can perform all the standard array operations on strings.
>>>
>>> If you need to mutate a string, then you can create a mutable `char[]` by doing `somestring.dup` as Dennis already mentioned.
>>
>> The problem I was having was actually that an opengl function (specifically glShaderSource) wouldn't accept strings. I'm still can't get it to work actually :P
>>
>> glShaderSource (uint, int, const(char*)*, const(int)*)
>>
>> This one function is a bugger, been going at this for hours.
>>
>>
>> On Tuesday, 2 June 2015 at 15:38:24 UTC, Meta wrote:
>>>
>>> Note that this will allocate a new garbage collected array.
>>
>> Thx for the heads up
>
> glShaderSource accepts an array of null-terminated strings. Try this:
>
>
> 	import std.string : toStringz;
>
> 	string sources = source.toStringz;
> 	int len = source.length;
>
> 	glShaderSource(id, sources, 1, &sources, &len);

src:

        string source = readText("test.glvert");
	
	const string sources = source.toStringz;
	const int len = source.length;
	
	GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
	
	glShaderSource(vertShader, 1, &sources, &len);

pt.d(26): Error: cannot implicitly convert expression (toStringz(source)) of type immutable(char)* to const(string)

pt.d(34): Error: function pointer glShaderSource (uint, int, const(char*)*, const(int)*) is not callable using argument types (uint, int, const(string)*, const(int)*)

-

I also tried passing the char array instead but no go.. What am I missing? :\
June 02, 2015
On Tuesday, 2 June 2015 at 16:41:38 UTC, Kyoji Klyden wrote:

> src:
>
>         string source = readText("test.glvert");
> 	
> 	const string sources = source.toStringz;
> 	const int len = source.length;
> 	
> 	GLuint vertShader = glCreateShader( GL_VERTEX_SHADER );
> 	
> 	glShaderSource(vertShader, 1, &sources, &len);
>
> pt.d(26): Error: cannot implicitly convert expression (toStringz(source)) of type immutable(char)* to const(string)
>
> pt.d(34): Error: function pointer glShaderSource (uint, int, const(char*)*, const(int)*) is not callable using argument types (uint, int, const(string)*, const(int)*)
>
> -
>
> I also tried passing the char array instead but no go.. What am I missing? :\

Oops, do `const immutable(char)* sources = source.toStringz` (or just use `auto sources = ...`).
« First   ‹ Prev
1 2 3 4 5