Re: convert string to ubyte[]

2017-11-11 Thread aki via Digitalmars-d-learn
On Saturday, 11 November 2017 at 15:48:59 UTC, Mike Parker wrote: auto s = "hello"; auto bytes = s.representation; https://dlang.org/phobos/std_string.html#.representation Thank you for the replay. Now I know. aki

convert string to ubyte[]

2017-11-11 Thread aki via Digitalmars-d-learn
Hello, This will be trivial question but I cannot figure out what's wrong. I want to convert string to an array of ubyte. import std.conv; void main() { auto s = "hello"; ubyte[] b = to!(ubyte[])(s); } It compiles but cause run time error: std.conv.ConvException@C:\APP\D\dmd2\w

Re: how to define a struct without padding

2017-05-24 Thread aki via Digitalmars-d-learn
On Thursday, 25 May 2017 at 01:34:35 UTC, Adam D. Ruppe wrote: align(1) struct NAME { align(1): members } My problem is resolved. Thank you for the quick reply. Thanks, Aki

how to define a struct without padding

2017-05-24 Thread aki via Digitalmars-d-learn
Hi, This is a code ported from some windows C program. It is the Windows WAVE file header definition. The field layout is the same with C, but it has extra 2 bytes at the end, sizeof(WAVEHEADER) in C is 46 while WAVEHEADER.sizeof in D is 48. Why it still have padding in spite of "align(1)" ? How

Re: what's the right way to get char* from string?

2016-05-05 Thread aki via Digitalmars-d-learn
On Thursday, 5 May 2016 at 11:35:09 UTC, Jonathan M Davis wrote: If you want a different mutability, then use the more general function std.utf.toUTFz. e.g. from the documentation: auto p1 = toUTFz!(char*)("hello world"); auto p2 = toUTFz!(const(char)*)("hello world"); auto p3 = toU

what's the right way to get char* from string?

2016-05-05 Thread aki via Digitalmars-d-learn
Hello, When I need to call C function, often need to have char* pointer from string. "Interfacing to C++" page: https://dlang.org/spec/cpp_interface.html have following example. extern (C) int strcmp(char* string1, char* string2); import std.string; int myDfunction(char[] s) { return strcmp

Re: how to declare C's static function?

2016-03-28 Thread aki via Digitalmars-d-learn
On Tuesday, 29 March 2016 at 01:04:50 UTC, Mike Parker wrote: On Monday, 28 March 2016 at 14:40:40 UTC, Adam D. Ruppe wrote: On Monday, 28 March 2016 at 04:53:19 UTC, aki wrote: So... You mean there are no way to declare functions without exporting the symbol? alas, no, even if it is private

Re: how to declare C's static function?

2016-03-28 Thread aki via Digitalmars-d-learn
On Monday, 28 March 2016 at 14:40:40 UTC, Adam D. Ruppe wrote: On Monday, 28 March 2016 at 04:53:19 UTC, aki wrote: So... You mean there are no way to declare functions without exporting the symbol? alas, no, even if it is private it can conflict on the outside (so stupid btw). Is it all th

Re: how to declare C's static function?

2016-03-27 Thread aki via Digitalmars-d-learn
On Monday, 28 March 2016 at 04:33:06 UTC, Rikki Cattermole wrote: You have two choices. Change the name in code (so manual mangling) or use pragma(mangle, ...) to change it instead. So... You mean there are no way to declare functions without exporting the symbol? There are so many instances to

Re: how to declare C's static function?

2016-03-27 Thread aki via Digitalmars-d-learn
On Monday, 28 March 2016 at 04:12:56 UTC, Rikki Cattermole wrote: Do you need it to use extern(C)? Because if you don't, just drop that. D's mangling will fix it. Yes, I do need extern(C) for some reason like: alias Hook = extern(C) void function(); void sethook(Hook func) { ... } ... setho

how to declare C's static function?

2016-03-27 Thread aki via Digitalmars-d-learn
Hello, When I porting legacy app. written in C to D, I have a problem. file a.d: extern (C) private void foo() {} file b.d: extern (C) private void foo() {} Error 1: Previous Definition Different : _foo In C language, "static void foo(){}" does not export the symbol out side the compilation

Re: efficient and safe way to iterate on associative array?

2016-03-04 Thread aki via Digitalmars-d-learn
On Friday, 4 March 2016 at 16:46:35 UTC, Steven Schveighoffer wrote: You cannot add or remove keys. You can modify values for existing keys. Note, in your code, this would not cause a problem, since setting hash to null just removes the reference from the local variable 'hash', it does not

efficient and safe way to iterate on associative array?

2016-03-04 Thread aki via Digitalmars-d-learn
Is it okay to modify associative array while iterating it? import std.stdio; void main() { string[string] hash = [ "k1":"v1", "k2":"v2" ]; auto r = hash.byKeyValue(); while(!r.empty) { auto key = r.front.key; auto value = r.front.value;

Re: question about the implementation of Variant

2016-01-04 Thread aki via Digitalmars-d-learn
Thank you, Jonathan. Now I understand. On Monday, 4 January 2016 at 17:34:47 UTC, Kapps wrote: union { ubyte[size] store; // conservatively mark the region as pointers static if (size >= (void*).sizeof) void*[size / (void*).sizeof] p; } Interesting to know the way to make G

question about the implementation of Variant

2016-01-03 Thread aki via Digitalmars-d-learn
Following function will return the reference to a object Foo embedded in a Variant. class Foo {} Variant fun() { Variant v; v = new Foo(); return v; } According to the source code of VariantN.opAssign, the assignment is done by: memcpy(&store, &rhs, rhs.sizeof); fptr =

Re: How to use core.thread.Thread

2015-07-17 Thread aki via Digitalmars-d-learn
On Friday, 17 July 2015 at 14:14:41 UTC, byron wrote: Since I have yet to use or see anyone use shared in a useful way I avoid it. It's one way to avoid it. So, you mean you always use send/receive when you need threading? I did small test to know the memory layout. import core.atomic; int foo

Re: How to use core.thread.Thread

2015-07-17 Thread aki via Digitalmars-d-learn
On Thursday, 16 July 2015 at 09:17:47 UTC, Daniel Kozák wrote: class DerivedThread : Thread { shared int count = 0; } I thought shared is only for whole of the object. auto thr = new DerivedThread(); Here, "thr" is not shared but it's member thr.count is shared? But if it's not shared,

Re: How to use core.thread.Thread

2015-07-16 Thread aki via Digitalmars-d-learn
On Thursday, 16 July 2015 at 08:21:26 UTC, maarten van damme wrote: Have you checked out std.parallelism and std.concurrency? I know std.concurrency to use spawn. If I cannot use Thread, I'll implement by spawn. But want to try Thread class because it seems similar to Java's Thread class. I don

Re: question about the semantics of unshared variables

2015-07-16 Thread aki via Digitalmars-d-learn
On Thursday, 16 July 2015 at 07:36:39 UTC, Jonathan M Davis wrote: Yes. Every thread gets a copy of the non-shared static variables, and all of the non-shared static constructors get run for each thread. - Jonathan M Davis Thank you for reply. Now i know. I did some test using C++ as you sai

How to use core.thread.Thread

2015-07-16 Thread aki via Digitalmars-d-learn
I can't resolve the compile errors: import core.thread; class DerivedThread : Thread { int count = 0; this() { super(&run); } private void run() { inc(); //testThread.d(8): Error: shared method testThread.DerivedThread.inc is not callable using a

Re: question about the semantics of unshared variables

2015-07-15 Thread aki via Digitalmars-d-learn
I noticed just making many threads cause an error. Are there any limit for the number of threads? import std.concurrency; import core.thread; void fun() { Thread.sleep(5000.msecs); } void testThread() { foreach(i; 0..2000) { spawn(&fun); } } core.thread.ThreadErro

question about the semantics of unshared variables

2015-07-15 Thread aki via Digitalmars-d-learn
I want to make sure about the semantics of unshared variables. import std.concurrency; import core.thread; ubyte[1024 * 1024] buf1MB; void fun() { Thread.sleep(5000.msecs); } void testThread() { foreach(i; 0..2000) { spawn(&fun); } } Are instances of buf1MB create

Re: how to manage the list of immutable objects

2015-07-14 Thread aki via Digitalmars-d-learn
On Tuesday, 14 July 2015 at 11:44:34 UTC, Steven Schveighoffer wrote: static Rebindable!(immutable(Foo))[string] map -Steve It all works. And I learned something from the source code of Rebindable. Thank you. Aki.

Re: how to manage the list of immutable objects

2015-07-14 Thread aki via Digitalmars-d-learn
On Tuesday, 14 July 2015 at 10:46:42 UTC, Andrea Fontana wrote: Trying on http://dpaste.dzfl.pl/ I don't see that error. Maybe you mean "const" and not "immutable" ? Andrea I used: DMD32 D Compiler v2.067.1 on Windows8 I believe there are no typo because it is copy-pasted. I noticed I can use

how to manage the list of immutable objects

2015-07-14 Thread aki via Digitalmars-d-learn
I like to create immutable object which is identified by name as it's key. And also need get() to look up named object which is already created. class Foo { static immutable(Foo)[string] map; string name; // and other attributes here... this(string name) immutable

Re: how to avoid "cycle detected"?

2015-07-02 Thread aki via Digitalmars-d-learn
On Thursday, 2 July 2015 at 17:21:03 UTC, Kapps wrote: An ugly solution, but the approach used in Phobos is to create something like a_init.d which a.d imports, provided that the static ctors don't actually rely on things from the static ctor of b. How about this idea? Allowing to define sub

Re: how to avoid "cycle detected"?

2015-07-02 Thread aki via Digitalmars-d-learn
On Wednesday, 1 July 2015 at 22:25:48 UTC, Jonathan M Davis wrote: On Wednesday, July 01, 2015 08:52:38 Steven Schveighoffer via Digitalmars-d-learn wrote: The runtime cannot introspect the code to detect the circular dependency, so it makes a conservative decision. I'm waiting on an introducti

how to avoid "cycle detected"?

2015-07-01 Thread aki via Digitalmars-d-learn
Following code causes run-time error. How can I use static this() without causing error? It's difficult to avoid this situation because actual code is more complex. file main.d: void main() { } file a.d: import b; class A { static this() {} }; file b.d: import a; class B { stati

Re: proper way to calculate toHash() for string

2015-06-30 Thread aki via Digitalmars-d-learn
On Tuesday, 30 June 2015 at 19:36:24 UTC, Jacob Carlborg wrote: On 30/06/15 16:19, aki wrote: Please suggest me if anyone have an idea. You can use TypeInfo.getHash [1] to get the hash of a given value. Something like: string a = "foo"; typeid(a).getHash(&a) [1] http://dlang.org/phobos/ob

proper way to calculate toHash() for string

2015-06-30 Thread aki via Digitalmars-d-learn
I would like to know proper way to calculate hash for given member fields. class Foo { int value; string str; override nothrow @safe size_t toHash() { // calculate hash based on field value. // value and str in this example. } } boo

Re: how to iterate on Array?

2015-06-30 Thread aki via Digitalmars-d-learn
On Sunday, 28 June 2015 at 10:16:47 UTC, Marc Schütz wrote: On Saturday, 27 June 2015 at 17:43:13 UTC, aki wrote: But when I compile it by DMD 2.062 on Windows it says: testArray.d(5): Error: cannot infer argument types (line 5 is at "foreach(i, v; a[]) {" ) 2.062 is really ancient, we're abo

Re: how to iterate on Array?

2015-06-27 Thread aki via Digitalmars-d-learn
On Saturday, 27 June 2015 at 18:32:10 UTC, Ali Çehreli wrote: On 06/27/2015 10:43 AM, aki wrote: You are rightly assuming that the loop counter is available for all container types. Unfortunately, it is the case only for arrays. Now I know. Thanks for it. aki.

how to iterate on Array?

2015-06-27 Thread aki via Digitalmars-d-learn
I want to print the contents of Array!int import std.stdio; import std.container; void pr(Array!int a) { foreach(i, v; a[]) { writeln("%4s: %s\n", i, v); } } But when I compile it by DMD 2.062 on Windows it says: testArray.d(5): Error: cannot infer argument type