Re: Password Storage

2016-01-03 Thread sarn via Digitalmars-d-learn

On Friday, 27 November 2015 at 00:17:34 UTC, brian wrote:
3) pre- or post-pend the salt to the password entered 
(apparently there is a difference??)
Sorry to revive an old thread, but I wrote a blog post about this 
question:

https://theartofmachinery.com/2016/01/03/What%20Difference%20Can%20Order%20Make%20When%20Hashing.html


Re: Functions that return type

2016-01-16 Thread sarn via Digitalmars-d-learn
On Saturday, 16 January 2016 at 21:22:15 UTC, data pulverizer 
wrote:
Is it possible to create a function that returns Type like 
typeof() does? Something such as:


Type returnInt(){
return int;
}


A type itself isn't a runtime value.  I think the closest thing 
is a TypeInfo object:


https://dlang.org/library/object/type_info.html
https://dlang.org/spec/expression.html#TypeidExpression


Re: how to mark an extern function @nogc?

2016-07-12 Thread sarn via Digitalmars-d-learn

On Tuesday, 12 July 2016 at 14:04:55 UTC, Seb wrote:
D is entirely driven by highly motivated volunteers. (this will 
change soon with the new D foundation)


I for one welcome our new D Foundation overlords.


Re: capture stdout or stderr

2017-02-02 Thread sarn via Digitalmars-d-learn

On Wednesday, 1 February 2017 at 01:08:19 UTC, Emil wrote:
is it possible to intercept the  STDOUT or STDERR and capture 
the output into a variable ?



some pseudocode to explain what I mean

string[] output_buffer;
stdout.capture_to(output_buffer);

writeln("test 1"); # not printed
writeln("test 2"); # not printed

stdout.release(output_buffer);

writeln("test 3"); # printed
writeln(output_buffer); # prints '["test 1","test 2"]'


If you *had* to, you should be able to hack it by reopening the 
file descriptors for standard output or error (at least on *nix), 
but is there a specific reason you want to do this, or do you 
just want formatted output to a variable?


Re: Safely moving structs in D

2017-01-23 Thread sarn via Digitalmars-d-learn

On Monday, 23 January 2017 at 22:26:58 UTC, bitwise wrote:

Is it ok to memcpy/memmove a struct in D?

Quote from here:
https://dlang.org/spec/garbage.html

"Do not have pointers in a struct instance that point back to 
the same instance. The trouble with this is if the instance 
gets moved in memory, the pointer will point back to where it 
came from, with likely disastrous results."


This seems to suggests it's ok to move structs around in memory 
without calling their postblit...but if this is the case, why 
does postblit even exist, if it's not strictly guaranteed to be 
called after the struct has been blitted?


You may need the postblit for a *copying* blit.  For example, if 
a struct does reference counting, the postblit will need to 
increment the count for the new copy.  It's a slightly different 
solution to what C++ solves with copy constructors, assignment 
operators, etc.  Compared to C++, the D approach is a bit simpler 
and trades off a little flexibility for more opportunities for 
the compiler to generate efficient code.


Here's the situation in C++, BTW:
http://en.cppreference.com/w/cpp/language/rule_of_three


Re: General performance tips

2017-01-23 Thread sarn via Digitalmars-d-learn

On Monday, 23 January 2017 at 12:13:30 UTC, albert-j wrote:
Well it is actually ODE solver from Numerical recipes 
(originally in C++) that I am trying to do in D. Code 
translation seems very straightforward. Maybe there's someone 
around who has done that already? There's not much object 
creation going on there, mostly loops over arrays, so I assume 
GC is not an issue.


It really is hard without seeing the code.  When you said "from 
Java", my first thought was that you'd want to convert a lot of 
classes to structs, but you say there's not much object creation 
going on.


"mostly loops over arrays" makes me think of bounds checking.  
Try benchmarking after compiling with -boundscheck=off to see if 
this is responsible for your speed difference.


By the way, the original C++ code is probably closer to good D 
code than the Java code.


If you really want fast numerical D code, take a look at Mir and 
ndslice.


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread sarn via Digitalmars-d-learn

On Saturday, 18 February 2017 at 21:09:20 UTC, ag0aep6g wrote:
Also, some threads online mention that if we do turn off GC, 
some of the core std libraries may not fully work. Is this 
presumption also correct?


Yes. Whenever a std function returns a new string or some such 
it's going to be GC-allocated.


This particular problem isn't so bad as it might sound because D 
string functions are based on ranges.


Re: Hello, folks! Newbie to D, have some questions!

2017-02-18 Thread sarn via Digitalmars-d-learn

On Saturday, 18 February 2017 at 20:15:55 UTC, timmyjose wrote:

Hello folks,


Hi :)

2. I am more interested in learning D as a pure systems 
programming language so that I can develop my own tools (not 
looking to develop an OS, just some grep-scale tools to start 
off with). In that regard, I have a few concerns about the GC. 
My rudimentary knowledge of the D ecosystem tells me that there 
is a GC in D, but that can be turned off. Is this correct? 
Also, some threads online mention that if we do turn off GC, 
some of the core std libraries may not fully work. Is this 
presumption also correct?


Okay, yes, it's easy to turn off or control the GC.  It's also 
easy to control memory allocation in general (unlike, say, Java, 
where it's practically impossible to do anything without writing 
"new").


Also, yes, a lot of the standard library doesn't work if you do 
that.  A lot does work, but a lot doesn't.  The biggest blocker 
is the use of exceptions, which currently rely on GC (though 
there's interest in changing that).


But I think the real answer to your question is in this thread:
https://forum.dlang.org/thread/o6c9tj$2bdp$1...@digitalmars.com
(Silicon Valley D Meetup - January 26, 2017 - "High Performance 
Tools in D" by Jon Degenhardt)


In this regard, I am curious to know if I would face any issues 
(with my intent in mind), or will I do just fine? If you could 
share your experiences and domains of use, that would also be 
very helpful for me. Secondly, how stable is the language and 
how fast is the pace of development on D?


When I first started using D about four years ago, it was easy to 
hit compiler bugs and basic things that didn't work.  It don't 
find that happens much nowadays when I'm doing everyday 
programming.


There's plenty of new stuff happening, like escape analysis, but 
the foundation is getting pretty good.  I think the biggest gap 
is the number of libraries compared to, say, Python, but 
personally I'm happy binding to C libraries, and there are plenty 
of them.


2. I am also curious as to what would be the best path for a 
complete beginner to D to learn it effectively? I am a 
relatively fast learner (and I learn better by context, as in, 
some core unifying idea described and then elucidated through 
big examples instead of learning in bits and pieces). How did 
you folks learn D? I'm sure hearing your experiences would be 
helpful too. Are there any books/video tutorials that you would 
recommend (aside from this site itself).


Some people have written tutorials.  It sounds like you're 
already experienced with programming, so the fastest way is 
probably to just dive in.  Get the basics from a small tutorial, 
then pick a small project (or some practice programming problems) 
and start coding with the standard library docs on hand :)


4. I have heard good reports of D's metaprogramming 
capabilities (ironically enough, primarily from a thread on the 
Rust user group), and coming from a Common Lisp (and some 
Racket) background, I am deeply interested in this aspect. Are 
D macros as powerful as Lisp macros? Are they semantically 
similar (for instance, I found Rust's macros are quite similar 
to Racket's)?


Lisp macros let you rewrite features at the interpreter level.  
Walter Bright has explicitly said he doesn't like that kind of 
macro (I don't think he even likes the C preprocessor's macros).


D's metaprogramming is more constrained in that sense, but it's 
powerful at code generation (see templates and the "mixin" 
keyword), compile-time code execution, and compile-time 
introspection.  Compile-time introspection is one of my favourite 
features.  If, for example, you need an array of all the names of 
single argument methods (or whatever) from a class, you can get 
it.


Take a look at ctRegex in the standard library for a great 
example of what can be done.


5. Supposing I devote the time and energy and get up to speed 
on D, would the core language team be welcoming if I feel like 
I can contribute?


I'm not the core team, but I'm confident the answer is yes :)


Re: Why do static arrays affect executable size?

2017-02-10 Thread sarn via Digitalmars-d-learn
On Friday, 10 February 2017 at 15:12:28 UTC, Jonathan M Davis 
wrote:
Module-level and static variables all get put in the 
executable. So, declaring a static array like that is going to 
take up space. A dynamic array would do the same thing if you 
gave it a value of that size. The same thing happens with 
global and static variables in C/C++.


An important difference with C/C++ in this case is that D floats 
are initialised to NaN, not 0.0.  In binary (assuming IEEE 
floating point), 0.0 has an all-zero representation, but NaNs 
don't.  Therefore, in C/C++ (on most platforms), 
default-initialised floats can be allocated in the BSS segment, 
which doesn't take up executable space, but in D, 
default-initialised floats have to be put into the compiled 
binary.


If you explicitly initialise the array to all 0.0, you should see 
it disappear from the binary.


Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-28 Thread sarn via Digitalmars-d-learn
On Tuesday, 28 February 2017 at 07:41:36 UTC, Christian Köstlin 
wrote:
As I understand the only difference between assert and enforce 
is, that

assert is not compiled into releases?

Thanks!
Christian


Pretty much so.  The intention is that assert means something 
that's supposed to be true (and can be assumed in release) while 
enforce means something you want to be true (but can't guarantee).


E.g., you can assert that a list is sorted after running heapsort 
on it, but you need to enforce that a file is in the correct 
format.


Re: pure functions

2016-09-12 Thread sarn via Digitalmars-d-learn
On Tuesday, 13 September 2016 at 03:33:04 UTC, Ivy Encarnacion 
wrote:

Can pure functions throw exceptions on its arguments?


You can throw exceptions for whatever reasons from a function 
marked pure:


void foo() pure
{
throw new Exception("nope");
}

void main()
{
foo();
}


Also, how can it perform impure operations?


Well, the point of pure is to restrict you from doing that.  Is 
there something specific you're trying to do?


Re: vibe.d maxRequestSize

2016-09-15 Thread sarn via Digitalmars-d-learn
I hope this isn't too obvious, but I have to ask because it's 
such a common gotcha:


Are you reverse proxying through a server like nginx by any 
chance?  There are default request size limits there.  (For nginx 
specifically, it's this one:

https://nginx.org/en/docs/http/ngx_http_core_module.html#client_max_body_size)


Re: Can vibe d leverage existing web technologies?

2016-09-16 Thread sarn via Digitalmars-d-learn
On Friday, 16 September 2016 at 12:46:34 UTC, Martin Tschierschke 
wrote:

The "only" problem is you have to build your layout twice,
in php and as diet template. :-(


You also have to manage URLs across different codebases.  I'd 
recommend against splitting up a frontend like that because it 
adds a lot of extra overhead.  But creating a new backend server 
isn't so expensive (not free, but not so expensive).  For 
example, I once wrote a thin proxy server in node.js for a 
third-party service that only supported node.js.  It wasn't as 
nice as a native solution would have been, but, hey, it worked.


Of course, with PHP it doesn't have to be a server.  A script 
might be simpler.


Re: What blogs about D do you read?

2016-09-20 Thread sarn via Digitalmars-d-learn

Don't forget the Planet D aggregator :)
http://planet.dsource.org/

Here's my contribution:
https://theartofmachinery.com/tags/dlang/


Re: Sockets and using them...

2016-11-06 Thread sarn via Digitalmars-d-learn

On Sunday, 6 November 2016 at 06:02:48 UTC, Era Scarecrow wrote:
 So I've got a project where I want to create basically a 
decentralized chat program where every program is a host and a 
client. When you connect all connections can go through to 
route the chat to everyone else.


 So to make this work I've looked over the sockets package and 
I don't quite follow how you'd make it so it works a lot like a 
web browser, aka when you get a connection you redirect to a 
different port so you have 1 listening port for new connections 
so it can act as a server. What settings or configuration would 
I need to be able to do that?


Web browsers do redirects at the application level.  It's in 
HTTP, which is a protocol built on top of the layer sockets 
provide.


It sounds like you want to do the same thing: design a protocol 
that includes some kind of message that's agreed to mean, "Yo, 
make a new connection on this port."  Basically, any 
off-the-shelf textbook on network protocol design will help you.


One thing I can tell you now, though, is that NATs will be your 
big problem if you try to deploy your system for real.  Most 
machines still use IPv4 and don't have publicly accessible IP 
addresses, so they use network address translation to access the 
internet.  Quirks of different NAT implementations cause huge 
pain to everyone who tries to deploy a peer-to-peer system.


But don't let me put you off :)


Re: Calling std.variant.visit from a pure function

2016-11-04 Thread sarn via Digitalmars-d-learn

On Friday, 4 November 2016 at 02:56:07 UTC, Paul Backus wrote:
When I compile this (using DMD 2.069 on Debian Linux), I get an 
error saying that I can't call visit from a pure function. This 
is surprising, since all visit does (in theory) is call the 
provided functions, and all of _them_ are pure.


My question is, is this an unavoidable limitation of visit's 
implementation, or is there a way to work around this?


I get the same error with 2.071.  (I haven't installed the new 
dmd yet.)


I suggest trying it with the latest dmd and filing a bug report.  
Taking a quick look at the library code 
(https://github.com/dlang/phobos/blob/master/std/variant.d), it 
*seems* like everything uses templates and functions returning 
auto, so the pureness of visit should be inferred if possible.


Re: Parse a String given some delimiters

2016-10-30 Thread sarn via Digitalmars-d-learn

On Sunday, 30 October 2016 at 20:50:47 UTC, Alfred Newman wrote:

Hello,

I'm migrating some Python code to D, but I stuck at a dead 
end...


Sorry to provide some .py lines over here, but I got some 
doubts about the best (fastest) way to do that in D.


The "splitter" generic function sounds like what you want.  The 
basic versions use a fixed separator, but you can make more 
complex separators using either the regex version, or the 
function version.  The function version is simplest for what 
you're doing.  Check out the first example here:

https://dlang.org/phobos/std_algorithm_iteration.html#.splitter.3

(You'd use "among" instead of plain ==)

Also check out "walkLength" for getting the number of tokens.

However, if you really care about speed, I suggest changing the 
API.  With your API, if you want to get multiple tokens from a 
string, you have to split the string every single time.  Why not 
just return the full range?  You can use "array" to return a 
proper array instead of an ad hoc range struct.


Re: general questions about static this() at module level

2016-10-30 Thread sarn via Digitalmars-d-learn

On Monday, 31 October 2016 at 04:35:35 UTC, WhatMeWorry wrote:
First, are there any other languages that has this feature?  
The few I know, certainly don't.


I've seen hacks to do the same thing in C++.  They're not pretty, 
though.


And how would you compare and contrast these this(s) with those 
of structs and classes?
Like, are they especially good for certain D idioms, particular 
cases, or are they good in general?


Class/struct static constructors are good for initialising 
class/struct static data.  Module static constructors are good 
for initialising module "static data" (i.e., globals).  They're 
especially handy for initialising immutable global data (which is 
the kind of global data D encourages).


BTW, immutable data is shared between threads, so you should use 
"shared static this" to initialise it.  Regular static 
constructors are executed per thread.


Re: [Semi-OT] I don't want to leave this language!

2016-12-05 Thread sarn via Digitalmars-d-learn

On Monday, 5 December 2016 at 17:18:25 UTC, e-y-e wrote:
Currently I have been learning D for about a year and a half. 
This may seem like a short time, but this is the longest I have 
stuck with any language. I have only been learning for 4 years 
and I am currently in university studying first year of 
computer systems engineering.

...
Does anyone have any advice for me?


Honestly, I recommend just learning C and C++.  Especially C if 
you're into low level stuff.  You won't just broaden your job 
market, you'll learn stuff that will help you use D more 
effectively.  You don't have to think of it as "leaving" the 
language.


how can I replace some of the great things about D? Things like 
built-in unittests, sane static if, painless CTFE, ranges, or 
even just the DUB package manager/build tool.


You'll have to learn to do without them :)


Re: How to get dub to work when I have dlang installed in custom directory?

2017-03-03 Thread sarn via Digitalmars-d-learn

On Friday, 3 March 2017 at 20:35:04 UTC, Jamal wrote:

I have no idea what is is wrong and or how to fix it.

Any help?


It would be the alias.  When you're running dmd from your shell, 
you're using an alias that includes a bunch of flags to make dmd 
work.  When dub runs, it'll run the dmd executable directly, not 
your alias, and lose the flags.


You can get rid of the alias and use a dmd.conf instead.  Just 
make a file called "dmd.conf" in the same directory as your dmd 
executable, and put something like this in it:


[Environment32]
DFLAGS=this>


[Environment64]
DFLAGS="dlang_compile">


Re: TLS

2017-03-10 Thread sarn via Digitalmars-d-learn

On Friday, 10 March 2017 at 19:24:29 UTC, bauss wrote:
Mark your variables with __gshared. I would say shred, but it 
has some restrictions to it, where __gshared is the equivalent 
to global variables in C.


immutable variables are also not put in TLS.


Re: Writing pattern matching macros in D.

2017-03-05 Thread sarn via Digitalmars-d-learn

On Monday, 6 March 2017 at 02:20:02 UTC, Deech wrote:

Hi all,
I've been reading up on D's metaprogramming features and was 
wondering if it was possible to use them to add pattern 
matching to the language as a macro. The template mixin feature 
seems to require putting the new syntax in strings. I was 
hoping there's an alternative.

Thanks!
-deech


It isn't possible in the same way it is in, say, Rust.  This has 
come up before and Walter has strong opinions against that kind 
of rewriting macro.


This is what's currently implemented:
https://dlang.org/phobos/std_variant.html#visit


Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread sarn via Digitalmars-d-learn
On Monday, 27 February 2017 at 19:26:06 UTC, Christian Köstlin 
wrote:
How can I make sure, that the calculations are done at compile 
time?


If you ever have doubts, you can always use something like this 
to check:


assert (__ctfe);


Re: How to enforce compile time evaluation (and test if it was done at compile time)

2017-02-27 Thread sarn via Digitalmars-d-learn

On Tuesday, 28 February 2017 at 00:20:05 UTC, sarn wrote:
On Monday, 27 February 2017 at 19:26:06 UTC, Christian Köstlin 
wrote:
How can I make sure, that the calculations are done at compile 
time?


If you ever have doubts, you can always use something like this 
to check:


assert (__ctfe);


Sorry, "enforce" would more appropriate if you're really checking.


Re: std.math module

2017-08-06 Thread sarn via Digitalmars-d-learn

On Sunday, 6 August 2017 at 23:33:26 UTC, greatsam4sure wrote:

import std.math;
import std.stdio;

cos(90*PI/180) = -2.7e-20 instead of zero. I will appreciate 
any help. thanks in advance.


tan(90*PI/180) = -3.689e+19 instead of infinity. What is the 
best way to use this module


That's just floating point maths for you.  You're not putting 
exactly pi/2 into cos, just a good floating point approximation.  
What you're getting out isn't exactly 0, either, just a good 
floating point approximation.  (-2.7e-20 is really, really small.)


Here's a good talk from DConf 2016: 
https://www.youtube.com/watch?v=97bxjeP3LzY


If you need exact maths, you'll need a symbolic manipulation 
library (never used one in D, but there was a discussion recently 
https://forum.dlang.org/thread/ghihookwgzxculshi...@forum.dlang.org).  You don't need this for most practical applications, though.


Re: BetterC and TypeInfo Question

2017-06-22 Thread sarn via Digitalmars-d-learn
Currently a lot of language features generate dependencies on 
TypeInfo, arguably more than needed, but this is changing.  Some 
examples are in this DConf 2017 talk:


https://www.youtube.com/watch?v=endKC3fDxqs

Also, the way the language is designed right now, all modules are 
responsible for containing TypeInfo instances for all their 
types, in case other modules *might* need them.  So, if you 
define a plain old data struct, for example, you get TypeInfo and 
its runtime dependencies.  This isn't a requirement, and Adam has 
just patched DMD to make -betterC leave out the TypeInfo.


But, even in C++, RTTI is necessary for dynamic casting... so 
how is D going to address that? I think it is necessary, but 
can prolly be quite minimal. I will think about it more later.


Does it matter?  C++ programmers already accept that RTTI is 
needed for certain dynamic features.


Re: ESR on post-C landscape

2017-11-16 Thread sarn via Digitalmars-d-learn
On Thursday, 16 November 2017 at 11:52:45 UTC, Ola Fosheim 
Grostad wrote:

On Thursday, 16 November 2017 at 11:24:09 UTC, codephantom
I would never say OO itself is a failure. But the idea that is 
should be the 'primary focus of program design' .. I think 
that is a failure...and I think that principle is generally 
accepted these days.


Uhm, no? What do you mean by 'primary focus of program design' 
and in which context?


In the 90s (and a bit into the 00s) there was a pretty extreme 
"everything must be an object; OO is the solution to everything" 
movement in the industry.  Like most tech fads, it was associated 
with a lot of marketing and snake oil from people promising 
anything managers would pay money to hear (e.g., "use OO and your 
projects will be made up of reusable objects that you can simply 
drop into your next project!").


Look around most programming languages today and you'll see 
objects, so in that sense OOP never failed.  What failed was the 
hype train.  It's no different from most other tech fads (except 
XML has declined drastically since the hype passed).


Re: ESR on post-C landscape

2017-11-16 Thread sarn via Digitalmars-d-learn
On Tuesday, 14 November 2017 at 09:43:07 UTC, Ola Fosheim Grøstad 
wrote:
ESR got famous for his cathedral vs bazaar piece, which IMO was 
basically just a not very insightful allegory over waterfall vs 
evolutionary development models, but since many software 
developers don't know the basics of software development he 
managed to become infamous for it…


Everything ESR says is worth taking with a good dose of salt, but 
his "The Art of Unix Programming" isn't a bad read.


Re: using wkhtmltopdf with D

2018-05-27 Thread sarn via Digitalmars-d-learn

On Monday, 28 May 2018 at 01:28:10 UTC, Dr.No wrote:
What's likely the reason of the crash? mismatch between D and C 
memory alignment?


From an ABI point of view, the raw pointers won't care about the 
memory structure they point to.  The function call is the only 
thing that depends on the binary interface.


If you translate the code into C, does it work?


Re: How can I point an array to existing data in memory while using Better C?

2018-07-08 Thread sarn via Digitalmars-d-learn

On Sunday, 8 July 2018 at 21:11:53 UTC, Stijn Herreman wrote:

On Sunday, 8 July 2018 at 20:27:34 UTC, Stijn Herreman wrote:
I should point out that I only have a vague idea of what I'm 
doing, I tried things until it compiled and worked (at first 
glance). If there are any docs that properly explain the 
casting of pointers, I'd appreciate the links.


I found the following works as desired:

environment.d

public __gshared header* GPT_header;
public __gshared partition_entry* GPT_entries;

main.d

GPT_header = cast(header*)0x7e00;
GPT_entries = cast(partition_entry*)0x8000;

That still lets me access GPT_entries with an index, e.g. 
GPT_entries[0]. Is this how it's supposed to be done, or is 
there a better way still?


You can also easily make slices out of pointers.  E.g.:

// Some backing memory
int[10] x;
// Take a pointer
int *xp = x.ptr;
// Convert pointer to D slice
int[] x2 = xp[0..10];
assert (x2.length == 10);

(Of course, you don't need to break it into steps in real code, 
and it works fine in betterC.)


There's still not a lot of info out there about doing betterC, 
but maybe you can find some examples in the following code.  It 
works without any druntime at all, including TypeInfo.  This is 
the entry point after a bootloader loads the D code and enters 
32b protected mode:

https://gitlab.com/sarneaud/xanthe/blob/master/src/os/drivers-bios.d#L198


Re: Strange behavior using array of structures

2018-04-04 Thread sarn via Digitalmars-d-learn

On Wednesday, 4 April 2018 at 10:00:18 UTC, Orfeo wrote:

  foreach (l; log) {
 l.run;
  }


Try making this "foreach (ref l; log) {".

Structs are value types in D, so by default they're copied when 
you assign them to another variable (in this case l).  That means 
run() is modifying a copy that gets thrown away each time.  "ref 
l" makes l into a reference to the original struct instance.


Re: How does buffering actually work?

2019-02-28 Thread sarn via Digitalmars-d-learn
On Thursday, 28 February 2019 at 21:17:23 UTC, Cleverson Casarin 
Uliana wrote:
It works almost perfectly, except that it doesn't wait for my 
first Enter after printing "First name: value1". Rather, it 
prints both "First name: value1" and "First name: value2" 
together on the same line, then it starts to behave as 
expected, e.g. printing one line at a time and waiting for me 
to press Enter.


Perhaps that happened with some other variation of the code.  The 
code you wrote shouldn't work like that (it doesn't for me when I 
tried, at least).


Ali has some good answers for fixing your code.  (readf("\n") 
also works, BTW.)  Hopefully this helps with the "How does 
buffering actually work?" question:


D uses the system's standard C library for IO, like most 
programming languages do, so IO buffering isn't fundamentally 
different (but some high-level functions might have different 
behaviour).


The standard C library provides buffered IO for input and output. 
 By default terminal IO is line-buffered (not sure if that's all 
systems), so you might see delays up until a newline, but 
line-by-line IO won't notice the buffering.


What happens here?

write()
read()
write()
read()

The first write goes to the output buffer.  If the buffer ever 
gets full (or has a newline in the case of line buffering), the 
data gets flushed to the real output.


At the read, it's possible there's still some data in the output 
buffer that's not flushed.  If needed, you can explicitly call 
flush() to make sure there isn't.  If there happens to already be 
data in the read buffer, read() will take as much as it needs to. 
 If there isn't enough, then real input will happen, and the call 
will block until data comes in.  The real read will ask for a 
chunk of data, which will often be more than the read() call 
needs.  The remainder gets put into the buffer (that's what it's 
for).  (The kernel and libc actually both have IO buffers.)


In any case, the second write won't happen until the read has 
finished.


Rinse and repeat for the remaining lines.


Re: Should D file end with newline?

2019-02-16 Thread sarn via Digitalmars-d-learn
On Friday, 15 February 2019 at 13:14:47 UTC, Patrick Schluter 
wrote:
A lots of fgets() based tools on Unix systems fail to read the 
last line if it doesn't contain a line feed character at the 
end. Afaicr glibc implementation does not have that problem but 
a lot of other standard C libs do.
When we were still on Solaris we had to be very careful with 
that, as strange things could happen when using sed, awk, wc 
and a lot of other standard Unix commands.
Now that we have switched to Linux we don't have the issue 
anymore.


That makes sense.  I guess I'm spoiled by GNU.


Re: Should D file end with newline?

2019-02-09 Thread sarn via Digitalmars-d-learn

On Saturday, 9 February 2019 at 21:19:27 UTC, Victor Porton wrote:

ISO C++ specifies that the C++ file must end with a newline.

Should D file end with newline, too?


I'm sure you could mostly get away without one, but POSIX says 
that all text files should end with a newline.  There are some 
POSIX tools that don't work properly without the final newline.


Re: Should D file end with newline?

2019-02-12 Thread sarn via Digitalmars-d-learn
On Tuesday, 12 February 2019 at 20:03:09 UTC, Jonathan M Davis 
wrote:

So, I'd say that it's safe to say that dmd
The whole thing just seems like a weird requirement that really 
shouldn't be there,


Like I said in the first reply, FWIW, it's a POSIX requirement.

Turns out most tools don't care (and dmd is apparently one of 
them).  If you want an easy counterexample, try the wc command 
(it miscounts lines for non-compliant files).  I've never seen 
that break an actual build system, which is why I said you could 
mostly get away with it.  On the other hand, being 
POSIX-compliant always works.


it matters even less if text editors are automatically 
appending newlines to files if they aren't there whether they 
show them or not, since if that's the case, you'd have to 
really work at it to have files not ending with newlines anyway.


There are definitely broken text editors out there that won't add 
the newline (can't think of names).  Like Jacob Carlborg said, 
Github flags the files they generate.


hexdump shows a newline followed by a null character followed 
by a newline after the carriage return.


hexdump is printing little-endian 16b by default, so I think 
that's just two newlines followed by a padding byte from hexdump. 
 Try using the -c or -b flag and you probably won't see any null 
byte.


Curiously, if I create a .cpp or .c file with vim and have it 
end with a curly brace, vim _does_ append a newline followed by 
a null character followed by a newline at the end of the file. 
So, I guess that vim looks at the extension and realizes that 
C/C++ has such a requirement and takes care of it for you, but 
it does not think that .d files need them and adds nothing 
extra for them. It doesn't add anything for a .txt file when I 
tried it either.


Are you sure?  vim is supposed to add the newline for all text 
files because that's POSIX.  It does on my (GNU/Linux) machine.


Re: What are some ways to get more strict type-checking?

2019-05-05 Thread sarn via Digitalmars-d-learn

On Monday, 6 May 2019 at 02:02:52 UTC, Devin wrote:
Recently, I poorly refactored some code, which introduced an 
obvious bug.  But to my astonishment, the broken code compiled 
without any warnings or notifications.  A minimum example is 
shown below:


alias ID = uint;
...


alias doesn't create a distinct type, but maybe Typedef from 
Phobos is what you want:


https://dlang.org/library/std/typecons/typedef.html


Re: Linking D Runtime

2019-09-03 Thread sarn via Digitalmars-d-learn

On Saturday, 24 August 2019 at 02:10:19 UTC, Jonathan Levi wrote:
I would love a more portable solution though.  This should work 
for now.


How are you building the D code?  It should be possible to build 
a library (with -lib and/or -shared) that statically includes the 
runtime and Phobos.


If you want to dynamically link the standard libraries, then you 
shouldn't need to specify the full path (just "phobos2" and 
"druntime") as long as they're installed in one of the system's 
library search paths (see /etc/ld.so.conf on GNU/Linux).


Re: canFind all elements in a array.

2020-11-10 Thread sarn via Digitalmars-d-learn

On Tuesday, 10 November 2020 at 08:19:15 UTC, Vino wrote:

foreach(i; data2[]) {
   if(data1[].canFind(i[0])) {
 writeln(i[1]);
  }
}


This is iterating over all the elements in data2 and outputting 
some of them, so the output will never be longer than data2.


It looks like you want to iterate over data1.  Something like 
this:


foreach(i; data1[]) {
  auto result = data2[].find!((p, x) => p[0] == x)(i);
  if (!result.empty) writeln(result.front[1]);
}

However, you could also use an associative array for data2:

string[string] data2 = [
  "DEV Systems": "DEV Cluster",
  "QAS Systems": "QAS Cluster",
];

foreach (i; data1[]) {
  if (auto v = i in data2) writeln(*v);
}

The "in" operator returns a pointer to the value in data2 at 
index "i", or else a null pointer.


See more info here:
https://ddili.org/ders/d.en/aa.html
https://dlang.org/spec/hash-map.html



Re: Druntime without pthreads?

2020-10-20 Thread sarn via Digitalmars-d-learn

On Tuesday, 20 October 2020 at 16:58:12 UTC, Severin Teona wrote:

Hi guys.

I have a curiosity, regarding [1] - I had encountered some 
"undefined reference" errors when trying to link the druntime 
(compiled for an embedded architecture) without some 
implementation of the POSIX thread calls (and other stuff too).


My curiosity is what would change if I removed from the 
druntime everything that has to do with mutexes or threads. 
Would it be possible for the druntime to run and work properly 
on a microcontroller - where those concepts are not necessary? 
Could I just remove everything about synchronisation from the 
druntime, and classes or Garbage Collector to still work 
properly?


[1]: 
https://forum.dlang.org/post/erwfgtigvcciohllv...@forum.dlang.org


An alternative would be to link to a custom library that 
implements the pthreads ABI, but stubs everything out in a way 
that makes sense on a single-threaded microcontroller.


I wanted to do that once for another project.  I never got it to 
work for unrelated reasons, so I can't say how well the D runtime 
handles it, sorry.  It's probably an easier approach than 
maintaining your own fork of the runtime, though.


Re: GC memory fragmentation

2021-04-16 Thread sarn via Digitalmars-d-learn

On Tuesday, 13 April 2021 at 12:30:13 UTC, tchaloupka wrote:
Some kind of GC memory dump and analyzer tool as mentioned 
`Diamond` would be of tremendous help to diagnose this..


I've used bpftrace to do some of that stuff:
https://theartofmachinery.com/2019/04/26/bpftrace_d_gc.html