On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer
wrote:
[...]
This worked for me:
```d
char[4] fourC(string s)
{
if(s.length >= 4)
return s[0 .. 4];
Silent truncation? Non-ASCII chars?
char[4] res = 0;
According to [1], [2] or [3] that should read
```
c
On Friday, 26 May 2023 at 13:18:15 UTC, Steven Schveighoffer
wrote:
This worked for me:
```d
char[4] fourC(string s)
{
if(s.length >= 4)
return s[0 .. 4];
char[4] res = 0;
res[0 .. s.length] = s;
return res;
}
```
Sometimes I forget that the return does an impli
On 5/26/23 8:19 AM, realhet wrote:
Hello,
Is there a way to do it nicer/better/faster/simpler?
```
char[4] fourC(string s)
{
uint res;//Zero initialized, not 0xff initialized.
auto cnt = min(s.length, 4),
p = cast(char[4]*)();
(*p)[0..cnt] = s[0..cnt];
return
Hello,
Is there a way to do it nicer/better/faster/simpler?
```
char[4] fourC(string s)
{
uint res;//Zero initialized, not 0xff initialized.
autocnt = min(s.length, 4),
p = cast(char[4]*)();
(*p)[0..cnt] = s[0..cnt];
return *p;
}
```
I tried
On Wednesday, 11 March 2020 at 16:10:48 UTC, 9il wrote:
On Wednesday, 11 March 2020 at 16:07:06 UTC, Abby wrote:
What is the proper way to get char* from string which is used
in c functions? toStringz does returns:
/usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo
cannot be used
On 12/03/2020 5:07 AM, Abby wrote:
What is the proper way to get char* from string which is used in c
functions? toStringz does returns:
/usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo cannot be
used with -betterC
and I think string.ptr is not safe because it's not zero
On Wednesday, 11 March 2020 at 16:07:06 UTC, Abby wrote:
What is the proper way to get char* from string which is used
in c functions? toStringz does returns:
/usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo
cannot be used with -betterC
and I think string.ptr is not safe because
What is the proper way to get char* from string which is used in
c functions? toStringz does returns:
/usr/include/dmd/phobos/std/array.d(965,49): Error: TypeInfo
cannot be used with -betterC
and I think string.ptr is not safe because it's not zero
termined. So what should I do? realloc
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:
> >> [...]
&g
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
e 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
get in d
> 0xABCDEF123abcdef12345678909832190 instead of 4D77EB
>
>
> // D
>
> void test(string test){
> char* testi = cast(char*)(test);
> writeln(testi);
>
> }
>
>
> void main()
> {
> test("0xABCDEF123abcdef12345678909832190");
>
){
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
On Tuesday, 9 May 2017 at 07:59:19 UTC, Stanislav Blinov wrote:
On Tuesday, 9 May 2017 at 07:50:33 UTC, David Zhang wrote:
If indeed there is no way to avoid allocation, do the
allocations have to remain 'alive' for the duration of the
instance? Or can I deallocate immediately afterwards? I
On Tuesday, 9 May 2017 at 07:50:33 UTC, David Zhang wrote:
If indeed there is no way to avoid allocation, do the
allocations have to remain 'alive' for the duration of the
instance? Or can I deallocate immediately afterwards? I can't
seem to find it in the Vulkan spec.
2.3.1. Object
in a
separate parameter.
import std.algorithm.iteration: map;
import std.array: array;
import std.conv: to;
import std.string: toStringz;
string[] strs = ["foo", "bar", "baz"];
/* convert string[] to char*[]: */
immutable(char)*[] chptrs = strs.map!to
, tightly packed. A
`string[]` isn't that. A single `string` is a pointer-and-length pair.
So a `string[]` has pointers and lengths alternating in memory.
Casting from `string[]` to `char*[]` means reinterpreting string lengths
as pointers. That's not what you want. When dereferencing those fake
On 09/05/2017 5:22 AM, David Zhang wrote:
Hi,
I'm playing around with Vulkan, and part of its initialization code
calls for an array of strings as char**. I've tried casting directly
(cast(char**)) and breaking it down into an array of char*s (char*[])
before getting the pointer to its first
Hi,
I'm playing around with Vulkan, and part of its initialization
code calls for an array of strings as char**. I've tried casting
directly (cast(char**)) and breaking it down into an array of
char*s (char*[]) before getting the pointer to its first element
([0]). It provides the correct
On Tuesday, 29 November 2016 at 15:01:37 UTC, Anders S wrote:
Thanks guys for a really quick answer !!
OK, a little bit awkward to use but getting there
posting a new question about char * to struct ;)
Thanks
/anders
Also:
import std.conv : text;
string temp = "This is a number";
string
Thanks guys for a really quick answer !!
OK, a little bit awkward to use but getting there
posting a new question about char * to struct ;)
Thanks
/anders
314356);
but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*)
format, ...) is not callable using argument types (char[80],
string, int)
because in D arrays do not decay to pointers. To get a null
terminated string use toStringz
Nor does this work
char [50] temp = "
str);
but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*)
format, ...) is not callable using argument types (char[80],
string, int)
Nor does this work
char [50] temp = "This is a number";
string greeting5= temp~" "~314356;
w in Dlang and import core.stdc.string and
code:
char [80] str;
sprintf(str, "This is a number = %d", 314356);
writefln("%s", str);
but get error
Error: function core.stdc.stdio.sprintf (char* s, const(char*)
format, ...) is not callable using argument types (char[80],
str
On Wednesday, 21 September 2016 at 12:39:57 UTC, Johan Engelen
wrote:
It should really be mentioned in the documentation of
toHexString, with an actual example instead of a unittest.
Do you use my dpldocs.info? I add such notes there from time to
time:
On Wednesday, 21 September 2016 at 13:06:08 UTC, Adam D. Ruppe
wrote:
the variable you are assigning the result to never does
anything with regard to overloads or template args.
Gotcha, thanks.
On Wednesday, 21 September 2016 at 12:29:54 UTC, Johan Engelen
wrote:
Wouldn't something like this be possible?
`T toHexString(string toHexString(Order order =
Order.increasing, LetterCase letterCase = LetterCase.upper,
T)(.) if (T == string)`
I'm not sure what that's supposed to
On Wednesday, 21 September 2016 at 12:20:14 UTC, Adam D. Ruppe
wrote:
This is a pretty common pitfall (and IMO one of the most
egregious design flaws in the language), I see it all the time.
I write very little D code, so I guess it had to happen at some
point then. Man, this is really bad
On Wednesday, 21 September 2016 at 12:20:14 UTC, Adam D. Ruppe
wrote:
It is neither, the compiler chose the right overload (remember,
overloads are chosen based on the arguments alone, the type you
specify for the variable holding the return value isn't a
consideration there) and the
,
but the language allows you to implicitly slice that into a
pointer (mistaken design in any case, doubly so since it a stack
pointer)... and moreover it is implicitly cast to immutable!
So it will implicitly cast that char[x] to string in a LOT of
places... and it is almost always wrong
hobos bug (overloads don't work like that).
How are template overloads involving `string` and
`char[constant]` return values supposed to work?
Still can't believe I am the first one to run into this, what am
I doing wrong?
-Johan
the type information, which is why inout
exists -
Well, I never got inout until now, thanks!
[...]
I don't know what you're using in Phobos that takes string and
returns char[]. That implies an allocation, and if the function
is pure, char[] may have been selected, because it could be
implicitly
When a string is not an in parameter, it can't be declared `in
char[]`.
Hi,
I saw from the forum that functions with string like arguments
better use `in char[]` instead of `string` type, because then it
can accept both string and char[] types.
But recently when actually using D, I found that many phobos
functions/constructors use `string`, while many returns
On Friday, March 18, 2016 08:24:24 Puming via Digitalmars-d-learn wrote:
> Hi,
>
> I saw from the forum that functions with string like arguments
> better use `in char[]` instead of `string` type, because then it
> can accept both string and char[] types.
>
> But recently whe
Must create a ticket for it ?
I think so. Unless others object in 10 minutes... :)
:-)
Done : https://issues.dlang.org/show_bug.cgi?id=14925
Thanks for your help
=
SwapStrategy.stable, Range)(Range range) if
(isBidirectionalRange!Range hasLvalueElements!Range)
inout.d(13): Error: template instance
std.array.replaceInPlace!(char, char[]) error instantiating
Don't understand why this doesn't work: it compiles fine and runs
perfectly if I change char[] by string
/array.d(2247):
std.array.replaceInPlace!(char, char[]).replaceInPlace(ref char[]
array, uint from, uint to, char[] stuff)
Must create a ticket for it ?
Don't understand why this doesn't work: it compiles fine and
runs
perfectly if I change char[] by string
You mean, this:
import std.array
This looks like a bug to me. The template constraints of the two
overloads are pretty complicated. This case should match only one of them.
On 08/15/2015 12:43 AM, TSalm wrote:
Don't understand why this doesn't work: it compiles fine and runs
perfectly if I change char[] by string
You mean
On 08/15/2015 01:47 AM, TSalm wrote:
Must create a ticket for it ?
I think so. Unless others object in 10 minutes... :)
In the other hand using string is not
efficient since this certainly make a copy of the original string.
Right ?
This is better to use replaceInPlace with char[], but
On Monday, 8 June 2015 at 09:54:28 UTC, Kagamin wrote:
On Sunday, 7 June 2015 at 17:41:11 UTC, Kyoji Klyden wrote:
Do you perchance have any links to learning resources for the
D runtime(aside from just the github repository), and also
maybe x86 architecture stuff? (I know intel has some 1000+
Ooooh okay, I'm starting to get it. I think this last question
should clear it up for me: When a string is made, how is the
struct Slice handled? What does ptr get assigned?
Hi,
Tell me, please, why this code works correctly always:
import std.stdio;
int n;
readf(%s\n, n);
string s, t;
readf(%s\n%s\n, s, t);
And this code works correctly is not always:
import std.stdio;
readf(%s\n, n);
char[200010] s, t;
scanf(%s%s, s.ptr, t.ptr);
Data is entered only in this
On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie wrote:
In C++ it is fully working:
char s[25], t[25];
scanf(%s%s, s, t);
Indeed.
Generate a 10-character string:
-
import std.range, std.stdio;
void main () {'a'.repeat (10).writeln;}
-
Try to copy it with D
On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote:
On Saturday, 21 March 2015 at 14:31:20 UTC, Dennis Ritchie
wrote:
In C++ it is fully working:
char s[25], t[25];
scanf(%s%s, s, t);
Indeed.
And why in D copied only the first 32767 characters of the
string? I'm more
In C++ it is fully working:
char s[25], t[25];
scanf(%s%s, s, t);
http://codeforces.com/contest/527/submission/10376381?locale=en
On Saturday, 21 March 2015 at 08:37:59 UTC, Dennis Ritchie wrote:
Tell me, please, why this code works correctly always:
[...]
And this code works correctly is not always:
import std.stdio;
readf(%s\n, n);
char[200010] s, t;
scanf(%s%s, s.ptr, t.ptr);
Please go into more detail about how
On Saturday, 21 March 2015 at 12:08:05 UTC, anonymous wrote:
Please go into more detail about how it doesn't work.
Task:
http://codeforces.com/contest/527/problem/B?locale=en
It works:
char[200010] s, t;
s = readln.strip;
t = readln.strip;
On 2015-03-21 at 16:05, Ivan Kazmenko wrote:
Generate a 10-character string
[...]
Try to copy it with D scanf and printf:
-
import std.stdio;
void main () {
char [10] a;
scanf (%s, a.ptr);
printf (%s\n, a.ptr);
}
-
Only 32767 first characters of the string are
On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote:
In what universe?! Which OS, compiler and architecture?
On Saturday, 21 March 2015 at 19:09:59 UTC, FG wrote:
In what universe?! Which OS, compiler and architecture?
Windows 8.1 x64, dmd 2.066.1:
import std.range, std.stdio;
void main
On Saturday, 21 March 2015 at 15:05:56 UTC, Ivan Kazmenko wrote:
Generate a 10-character string:
-
import std.range, std.stdio;
void main () {'a'.repeat (10).writeln;}
-
Try to copy it with D scanf and printf:
-
import std.stdio;
void main () {
char [10] a;
On 2015-03-21 at 21:02, Dennis Ritchie wrote:
In what universe?! Which OS, compiler and architecture?
Windows 8.1 x64, dmd 2.066.1:
That's strange. I cannot recreate the problem on Win7 x64 with dmd 2.066.1,
neither when compiled for 32- nor 64-bit. I have saved the a's to a file and
use
On Saturday, 21 March 2015 at 23:00:46 UTC, Ivan Kazmenko wrote:
To me, it looks like a bug somewhere, though I don't get where
exactly. Is it in bits of DigitalMars C/C++ compiler code
glued into druntime?
As far as I understand, the bug is in snn.lib's scanf.
snn.lib is Digital Mars's
On Saturday, 21 March 2015 at 16:34:44 UTC, Dennis Ritchie wrote:
And why in D copied only the first 32767 characters of the
string? I'm more days couldn't understand what was going on...
To me, it looks like a bug somewhere, though I don't get where
exactly. Is it in bits of DigitalMars
On 2015-03-21 at 22:15, FG wrote:
On 2015-03-21 at 21:02, Dennis Ritchie wrote:
In what universe?! Which OS, compiler and architecture?
Windows 8.1 x64, dmd 2.066.1:
That's strange. I cannot recreate the problem on Win7 x64 with dmd 2.066.1,
neither when compiled for 32- nor 64-bit. I have
On Saturday, 21 March 2015 at 23:00:46 UTC, Ivan Kazmenko wrote:
On Saturday, 21 March 2015 at 16:34:44 UTC, Dennis Ritchie
wrote:
And why in D copied only the first 32767 characters of the
string? I'm more days couldn't understand what was going on...
To me, it looks like a bug somewhere,
https://github.com/Hackerpilot/libdparse/blob/master/src/std/d/lexer.d#L1491
Of course consuming it dchar by dchar also works:
string s = `\tabŁŃ\r\nx`;
assert(parseDchar(s) == '\t');
assert(parseDchar(s) == 'a');
assert(parseDchar(s) == 'b');
assert(parseDchar(s) == 'Ł');
assert(parseDchar(s) == 'Ń');
assert(parseDchar(s) == '\r');
On 2015-02-09 at 03:40, Timothee Cour via Digitalmars-d-learn wrote:
Is there a simple way to parse a string as a char?
eg:
unittest{
assert(parseChar(`a`)=='a');
assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n
// should also work with other forms of characters, see
Is there a simple way to parse a string as a char?
eg:
unittest{
assert(parseChar(`a`)=='a');
assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n
// should also work with other forms of characters, see
http://dlang.org/lex.html
}
Note, std.conv.to doesn't work (`\n`.to!char
On 9/02/2015 3:40 p.m., Timothee Cour via Digitalmars-d-learn wrote:
Is there a simple way to parse a string as a char?
eg:
unittest{
assert(parseChar(`a`)=='a');
assert(parseChar(`\n`)=='\n'); //NOTE: I'm looking at `\n` not \n
// should also work with other forms of characters, see
' ]
I tried with all combinations of char[char[]] over string[char[]],
char[string] to string[string] with depending quotations 'abc' to abc.
My idea is just to print out the combinations to console. I'm not even
.sure, if it's a good idea to do so, but what makes me cry is, after all
trying, is that I
It seems you are right. I get the same error. You should open a
bug report for this.
But as long as it isn't fixed you can use an output via foreach.
import std.stdio;
void main() {
string[string] ch_Description = [
kill : kills a process,
pause : pauses
On 02.02.2013 16:06, Namespace wrote:
import std.stdio;
void main() {
string[string] ch_Description = [
kill : kills a process,
pause : pauses a process
];
// writeln(ch_Description); // does not work, but it should
foreach (string key, string value;
On 2013-02-02 16:06, Namespace wrote:
string[string] ch_Description = [
kill : kills a process,
pause : pauses a process
];
writeln(ch_Description); // does not work, but it should
Doesn't work in the current version? In DMD 2.060 it works.
On Saturday, 2 February 2013 at 16:06:09 UTC, FG wrote:
On 2013-02-02 16:06, Namespace wrote:
string[string] ch_Description = [
kill : kills a process,
pause : pauses a process
];
writeln(ch_Description); // does not work, but it should
Doesn't work in the current
Works now.
On 02/02/2013 08:00 AM, timewulf wrote:
After searching for the differences to my code, I found my fault:
It is not your fault. :)
Now I've just to look, how to declare this array in the way of public
constants.
You need 'static this()':
import std.stdio;
string[string] ch_Description;
On 03.02.2013 06:09, Ali Çehreli wrote:
You need 'static this()':
import std.stdio;
string[string] ch_Description;
static this()
{
ch_Description = [ kill : kills a process,
pause : pauses a process ];
}
void main() {
// ...
}
You can even make
On Fri, 23 Mar 2012 19:23:07 +0100, Ali Çehreli acehr...@yahoo.com wrote:
On 03/23/2012 08:48 AM, simendsjo wrote:
What's the best way to convert char** from string[]?
In C, char** communicates transfer of ownership. Is that what you are
trying to do? Are you going to pass a slice to a C
On Fri, 23 Mar 2012 17:09:18 +0100, bearophile bearophileh...@lycos.com
wrote:
On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote:
What's the best way to convert char** from string[]?
This is one way to do it:
import std.algorithm, std.array, std.string, core.stdc.stdio;
void main
On Fri, 23 Mar 2012 22:42:08 +0100, Andrej Mitrovic
andrej.mitrov...@gmail.com wrote:
On 3/23/12, bearophile bearophileh...@lycos.com wrote:
This is one way to do it:
immutable(char)** p = array(map!toStringz(data)).ptr;
This is asked so frequently that I think we could consider
On Sat, 24 Mar 2012 11:41:48 +0100, simendsjo simend...@gmail.com wrote:
On Fri, 23 Mar 2012 17:09:18 +0100, bearophile
bearophileh...@lycos.com wrote:
On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote:
What's the best way to convert char** from string[]?
This is one way to do
simendsjo:
Oh, I didn't find toStringz, so I turned it into:
It's in std.string.
auto c_strings = strings.map!(toUTFz!(char*)).array().ptr;
That's wrong syntax, that 2.059head doesn't enforce yet, map needs an ending ().
Bye,
bearophile
What's the best way to convert char** from string[]?
On Friday, 23 March 2012 at 15:48:12 UTC, simendsjo wrote:
What's the best way to convert char** from string[]?
This is one way to do it:
import std.algorithm, std.array, std.string, core.stdc.stdio;
void main() {
auto data = [red, yellow, green];
immutable(char)** p = array(map
On 03/23/2012 08:48 AM, simendsjo wrote:
What's the best way to convert char** from string[]?
In C, char** communicates transfer of ownership. Is that what you are
trying to do? Are you going to pass a slice to a C function to be filled
in by that C function?
Such functions usually assign
On 3/23/12, bearophile bearophileh...@lycos.com wrote:
This is one way to do it:
immutable(char)** p = array(map!toStringz(data)).ptr;
This is asked so frequently that I think we could consider adding it to Phobos.
such as this:
struct Info
{
char* name;
char* info;
}
I think it would be convenient if writef would call to!string for
char* parameters behind the scenes if %s is the format specifier.
I mean yes, you can use to!string in your code, or you can write D
classes that wrap C libraries, but when
On 9/11/10 3:00 PM, shd wrote:
Hello,
I'm having a problem in passing a value to char* expecting function
in D 2.0. Already tried:
to!(char*)(my string);
but it seems like there (Phobos) is no template like this. Then,
tried:
cast(char*)to!(char[])(my string)
which looked ok, but i think
to always copy the string, then char*
would likely be a better choice. But it could be that whatever issue made it so
that the non-copying version was commented out will be fixed at some point, and
toStringz() will once again cease to make a copy if it doesn't have to, at
which
point it would
Jonathan M Davis:
Well, if you look at toStringz()'s implementation, you may notice that
there's
commented out code which would not make a copy if there's a 0 in memory one
passed the end of the string. It would simply use that 0 as the end of the
const
char* and avoid the copy. That
Hello,
I'm having a problem in passing a value to char* expecting function
in D 2.0. Already tried:
to!(char*)(my string);
but it seems like there (Phobos) is no template like this. Then,
tried:
cast(char*)to!(char[])(my string)
which looked ok, but i think it's not a proper way to do
On 2010-09-11 15:13, Simen kjaeraas wrote:
Why does the function expect a char*? If it is an external C function,
and it might change the passed values, you should make a duplicate
mutable string, or use char[] in lieu of string.
If it is an external C function that will *not* change the passed
function,
and it might change the passed values, you should make a duplicate
mutable string, or use char[] in lieu of string.
If it is an external C function that will *not* change the passed
values, and you have write access to the D headers to interface to C,
use const char* instead. If no write
shd:
I'm having a problem in passing a value to char* expecting function
in D 2.0. Already tried:
to!(char*)(my string);
A solution, maybe correct:
import std.string: toStringz, indexOf;
import std.c.string: strlen;
import std.stdio: writeln;
void main() {
string s = my string
On Wed, 13 May 2009 16:42:51 -0400, Steven Schveighoffer
schvei...@yahoo.com wrote:
it is a documentation bug, this behavior is not allowed. Please submit a
bug to bugzilla: http://d.puremagic.com/issues/
On the other hand, string *literals* are implicitly castable to char *:
char *p = abc;
Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't
compile:
void main()
{
string str = abc;
char* p = str; // pointer to 1st element
}
Error: cannot implicitly convert expression (str) of type char[] to char*
I agree it shouldn't
Doctor J schrieb:
Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this doesn't
compile:
void main()
{
string str = abc;
char* p = str; // pointer to 1st element
}
Error: cannot implicitly convert expression (str) of type char[] to char
On Wed, 13 May 2009 14:28:46 -0400, Doctor J nob...@nowhere.com wrote:
Taken straight from http://www.digitalmars.com/d/1.0/arrays.html, this
doesn't compile:
void main()
{
string str = abc;
char* p = str; // pointer to 1st element
}
Error: cannot
90 matches
Mail list logo