Re: countUntil with negated pre-defined predicate?

2020-05-02 Thread Harry Gillanders via Digitalmars-d-learn

On Saturday, 2 May 2020 at 18:23:30 UTC, Robert M. Münch wrote:
Or is there an even better way to search for all "drawable 
unicode characters"?


This depends on what you classify as drawable, and what you 
consider to be a character (the joys of Unicode), and why you 
want to search for them anyway.


One way (I haven't verified this) could be to check if any of the 
code-points within a grapheme are graphical[1], and not 
white-space (and are not any other code-point you consider 
non-drawable).


Which could look like so:

import std.algorithm;
import std.range;
import std.uni;

	size_t drawableCharacterCount (CodePoints) (auto ref CodePoints 
codePoints)
	if (isInputRange!CodePoints && is(ElementType!CodePoints : 
dchar))

{
bool isDrawableCodePoint (dchar c)
{
return c.isGraphical() && !c.isWhite();
}

return codePoints.byGrapheme().count!(
g => g[].any!isDrawableCodePoint
);
}

[1]: 
https://www.unicode.org/versions/Unicode13.0.0/ch02.pdf#G286941


---
The source-code in this reply is available for use under the 
terms of Creative Commons CC0 1.0 Universal.





Re: How can I open a Binary EXE with Hexadecimal?

2020-05-02 Thread wolframw via Digitalmars-d-learn

On Saturday, 2 May 2020 at 21:05:32 UTC, Baby Beaker wrote:

save as "rb" again.


This will not work. To be able to write to a binary file, you 
will have to use "wb".


Re: How can I open a Binary EXE with Hexadecimal?

2020-05-02 Thread welkam via Digitalmars-d-learn

On Saturday, 2 May 2020 at 21:05:32 UTC, Baby Beaker wrote:
I need open a Binary EXE, it can be using "rb" mode and convert 
it to Hexadecimal for me make some changes and save as "rb" 
again. How can I make it? Thank you.


You dont convert binary data to hexadecimal. You display it as 
hexadecimal. If you want to print file contents to console in hex 
do something like this.


import std.stdio;
import std.algorithm;

auto fd = File("filename", "r"); //open file for reading
ubyte[] data; //a GC allocated buffer for data
fd.rawRead(data); //read the data

data[0..50].each!(n => stdout.writef("%X ",n));
/*take 50 elements from array and for each of them call lambda 
that converts ubyte to string in hex format and prints to the 
stdout*/


if you want to assign to this data you can do something like this

data[0] = Oxff;

And then write to file


How can I open a Binary EXE with Hexadecimal?

2020-05-02 Thread Baby Beaker via Digitalmars-d-learn
I need open a Binary EXE, it can be using "rb" mode and convert 
it to Hexadecimal for me make some changes and save as "rb" 
again. How can I make it? Thank you.


Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/2/20 3:08 PM, Robert M. Münch wrote:

On 2020-05-02 18:18:44 +, Steven Schveighoffer said:


On 5/2/20 4:44 AM, Robert M. Münch wrote:

How would that help, because the class instance is now unusable 
anyway. So I have it around like a zombie and others might think: 
"Hey you look normal, let's get in contact" and then you are doomed...


The difference is that if you use it, you get an error and a crash. If 
you clean up the memory, that memory could be reallocated to something 
else with a completely different type, and now you have memory 
corruption.


I didn't thought about the "memory is re-used" case here...

And how is the instance made unusable so that a crash happens (which I 
prefer too!)? Does .destroy zero the memory? Just curious how the crash 
situation is detected.




destroy sets all the values to the .init value. And it nulls the vtable 
pointer. So any virtual calls will crash with a segfault. non-virtual 
calls won't crash immediately, but generally there are few class calls 
that have all final calls.


And even if they do go through, the .init value should be harmless in 
terms of memory safety.


For reference, destroy calls this function on class instances (Same as 
GC cleanup) where p is really the class reference:


https://github.com/dlang/druntime/blob/999367be8fa5d13a718d951d67c3d580ca13aef1/src/rt/lifetime.d#L1414

You can see in the finally clause, the vptr is set to null.

-Steve


Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn

On 2020-05-02 18:18:44 +, Steven Schveighoffer said:


On 5/2/20 4:44 AM, Robert M. Münch wrote:

How would that help, because the class instance is now unusable anyway. 
So I have it around like a zombie and others might think: "Hey you look 
normal, let's get in contact" and then you are doomed...


The difference is that if you use it, you get an error and a crash. If 
you clean up the memory, that memory could be reallocated to something 
else with a completely different type, and now you have memory 
corruption.


I didn't thought about the "memory is re-used" case here...

And how is the instance made unusable so that a crash happens (which I 
prefer too!)? Does .destroy zero the memory? Just curious how the crash 
situation is detected.


--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: countUntil with negated pre-defined predicate?

2020-05-02 Thread Harry Gillanders via Digitalmars-d-learn

On Saturday, 2 May 2020 at 18:23:30 UTC, Robert M. Münch wrote:

This works:

countUntil!(std.uni.isWhite)("hello world"))

How can I switch this to (not working);

countUntil!(!std.uni.isWhite)("hello world"))

without having to write my own predicate?

Or is there an even better way to search for all "drawable 
unicode characters"?


std.functional.not can do this: 
https://dlang.org/phobos/std_functional.html#not


Re: countUntil with negated pre-defined predicate?

2020-05-02 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/2/20 2:23 PM, Robert M. Münch wrote:

This works:

 countUntil!(std.uni.isWhite)("hello world"))

How can I switch this to (not working);

 countUntil!(!std.uni.isWhite)("hello world"))

without having to write my own predicate?

Or is there an even better way to search for all "drawable unicode 
characters"?




Write your own predicate, it's not much different:

countUntil!(c => !std.uni.isWhite(c))("hello world")

-Steve


countUntil with negated pre-defined predicate?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn

This works:

countUntil!(std.uni.isWhite)("hello world"))

How can I switch this to (not working);

countUntil!(!std.uni.isWhite)("hello world"))

without having to write my own predicate?

Or is there an even better way to search for all "drawable unicode characters"?

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster



Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Steven Schveighoffer via Digitalmars-d-learn

On 5/2/20 4:44 AM, Robert M. Münch wrote:

On 2020-04-30 17:45:24 +, Steven Schveighoffer said:

You can use scope instead of auto, and it will then allocate the class 
on the stack, and destroy it as Ben Jones said. There is danger there, 
however, as it's very easy to store a class reference elsewhere, and 
then you have a dangling pointer.



Ok. Can't this be combined with some "don't let the refrence escape my 
function" feature of D?


I don't know. perhaps dip1000 helps here.




A safer thing to do is:


auto X = new MyClass();

scope(exit) destroy(X);


This runs the destructor and makes the class instance unusable, but does 
not free the memory (so any remaining references, if used, will not 
corrupt memory).



How would that help, because the class instance is now unusable anyway. 
So I have it around like a zombie and others might think: "Hey you look 
normal, let's get in contact" and then you are doomed...


The difference is that if you use it, you get an error and a crash. If 
you clean up the memory, that memory could be reallocated to something 
else with a completely different type, and now you have memory corruption.


-Steve


Re: sort a string

2020-05-02 Thread notna via Digitalmars-d-learn

On Friday, 1 May 2020 at 19:25:43 UTC, Steven Schveighoffer wrote:


Nice! Yeah, I was sloppy in my newsgroup coding, sorry.

One minor nit here, the to!(dchar[])(word.dup), the dup is not 
necessary, you are going to end up allocating a temporary array 
and throwing it away.


Just do word.to!(dchar[]). `to` takes care of all the 
formalities.


-Steve


THANK YOU for all the great hints and explanations here and in 
general in this NG, Steve!


No blaming for the "sloopy code" at all. I should have seen the 
missing [] by myself, but...


_AND_ btw. your hint with the "release" was key to the discussion 
/ solution anyhow! :)


Re: a function like writeln that returns a string rather than writes to a file

2020-05-02 Thread dan via Digitalmars-d-learn

On Saturday, 2 May 2020 at 10:36:47 UTC, Ali Çehreli wrote:
On 5/1/20 7:40 PM, dan wrote:> On Saturday, 2 May 2020 at 
02:29:43 UTC, H. S. Teoh wrote:

>> On Sat, May 02, 2020 at 02:22:42AM +, dan via
Digitalmars-d-learn
>> wrote:
>>> I'm looking for a function something like writeln or write,
but
>>> instead of writing to stdout, it writes to a string and
returns the
>>> string.
>> [...]
>>
>> import std.format : format;
>> string str = format("%s %s %s", obj1, obj2, obj3);
>>
>>
>> T
>
> Thanks HS!
>
> That looks like a good move, if format will do the string
conversion for
> me.
>
> But one thing that would be troublesome is that i would have
to make
> sure to count up the %s so that they match the number of
arguments.  I
> would like to do without that, just like writeln does.

If you can live with a mildly awkward way of passing it, 
format() can take the format string at compile time as well:


  string str = format!"%s %s %s"(obj1, obj2, obj3);

You get a compilation error if format specifications don't 
match the arguments. (There are bug reports about that check 
but it mostly works great.)


Ali


Thanks Ali.

That's also a good point, and would remove one of my qualms about 
all of the %s reps.


So if for any reason i cannot use the text function (or if i want 
to double check on the types of the objects) this would be a good 
thing to use.


dan


Re: Hided Subprocess in Dlang

2020-05-02 Thread Baby Beaker via Digitalmars-d-learn

On Saturday, 2 May 2020 at 15:42:20 UTC, Adam D. Ruppe wrote:

On Saturday, 2 May 2020 at 15:37:09 UTC, Baby Beaker wrote:
Error: none of the overloads of `spawnProcess` are callable 
using argument types `(string, File, File, File, Config)`, 
candidates are:


The example is prolly out of date

try

spawnProcess(program, null, Config.suppressConsole)


Working very well! Thank you!
A simple example using spawnShell just open notepad.exe without 
console.


spawnShell("notepad.exe", null, Config.suppressConsole);


Re: Hided Subprocess in Dlang

2020-05-02 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 2 May 2020 at 15:37:09 UTC, Baby Beaker wrote:
Error: none of the overloads of `spawnProcess` are callable 
using argument types `(string, File, File, File, Config)`, 
candidates are:


The example is prolly out of date

try

spawnProcess(program, null, Config.suppressConsole)


Re: Hided Subprocess in Dlang

2020-05-02 Thread Baby Beaker via Digitalmars-d-learn

On Saturday, 2 May 2020 at 15:20:36 UTC, Adam D. Ruppe wrote:

On Saturday, 2 May 2020 at 14:06:55 UTC, Baby Beaker wrote:

open the command prompt console running this other process.


when calling the functions pass Config.suppressConsole to it. 
like in the doc example here


http://dpldocs.info/experimental-docs/std.process.Config.html#suppressConsole


Error: none of the overloads of `spawnProcess` are callable using 
argument types `(string, File, File, File, Config)`, candidates 
are:


Re: Hided Subprocess in Dlang

2020-05-02 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 2 May 2020 at 14:06:55 UTC, Baby Beaker wrote:

open the command prompt console running this other process.


when calling the functions pass Config.suppressConsole to it. 
like in the doc example here


http://dpldocs.info/experimental-docs/std.process.Config.html#suppressConsole


Hided Subprocess in Dlang

2020-05-02 Thread Baby Beaker via Digitalmars-d-learn
I am creating a program in Dlang with graphical user interface. I 
want this program to run a hidden sub-process in the background. 
But when I click the program to run this process using spamShell 
or executeShell, open the command prompt console running this 
other process. Please help me.


Re: a function like writeln that returns a string rather than writes to a file

2020-05-02 Thread Ali Çehreli via Digitalmars-d-learn
On 5/1/20 7:40 PM, dan wrote:> On Saturday, 2 May 2020 at 02:29:43 UTC, 
H. S. Teoh wrote:

>> On Sat, May 02, 2020 at 02:22:42AM +, dan via Digitalmars-d-learn
>> wrote:
>>> I'm looking for a function something like writeln or write, but
>>> instead of writing to stdout, it writes to a string and returns the
>>> string.
>> [...]
>>
>> import std.format : format;
>> string str = format("%s %s %s", obj1, obj2, obj3);
>>
>>
>> T
>
> Thanks HS!
>
> That looks like a good move, if format will do the string conversion for
> me.
>
> But one thing that would be troublesome is that i would have to make
> sure to count up the %s so that they match the number of arguments.  I
> would like to do without that, just like writeln does.

If you can live with a mildly awkward way of passing it, format() can 
take the format string at compile time as well:


  string str = format!"%s %s %s"(obj1, obj2, obj3);

You get a compilation error if format specifications don't match the 
arguments. (There are bug reports about that check but it mostly works 
great.)


Ali



Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn

On 2020-04-30 17:45:24 +, Steven Schveighoffer said:

No, auto is declaring that there's about to be a variable here. In 
actuality, auto does nothing in the first case, it just means local 
variable. But without the type name, the type is inferred (i.e. your 
second example). This does not do any automatic destruction of your 
class, it's still left to the GC.


Ok, that was my understand too. As said, I found some older posts and 
was a bit confused...


You can use scope instead of auto, and it will then allocate the class 
on the stack, and destroy it as Ben Jones said. There is danger there, 
however, as it's very easy to store a class reference elsewhere, and 
then you have a dangling pointer.


Ok. Can't this be combined with some "don't let the refrence escape my 
function" feature of D?



A safer thing to do is:

auto X = new MyClass();
scope(exit) destroy(X);

This runs the destructor and makes the class instance unusable, but 
does not free the memory (so any remaining references, if used, will 
not corrupt memory).


How would that help, because the class instance is now unusable anyway. 
So I have it around like a zombie and others might think: "Hey you look 
normal, let's get in contact" and then you are doomed...


If your concern is guaranteeing destructors are run, that's what I 
would pick. If in addition you want guaranteed memory cleanup, then use 
scope (and be careful).


Ok, thanks.

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster

Re: Idomatic way to guarantee to run destructor?

2020-05-02 Thread Robert M. Münch via Digitalmars-d-learn

On 2020-04-30 17:04:43 +, Ben Jones said:

I think you want to use scope rather than auto which will put the class 
on the stack and call its destructor: 
https://dlang.org/spec/attribute.html#scope


Yes, thanks.

--
Robert M. Münch
http://www.saphirion.com
smarter | better | faster