Re: Orphan format arguments: args[0..1]

2018-12-16 Thread bauss via Digitalmars-d-learn

On Sunday, 16 December 2018 at 00:34:48 UTC, Ali Çehreli wrote:

This one confused me until I decided to talk to a rubber ducky:

import std.string;

void main() {
auto s = "%s is a good number".format(42);
}

Fine; it works... Then the string becomes too long and I split 
it:


auto s = "%s is a good number but one needs to know" ~
 " what the question exactly was.".format(42);

Now there is a compilation error:

  Orphan format arguments: args[0..1]

What? Is that a bug in format? It can't be because the string 
should be concatenated by the compiler as a single string, no? 
No: operator dot has precedence over ~, so format is applied to 
the second part of the string before the concatenation. Doh! 
This puzzled me a lot.


Anyway, the solution, once again, is to use parentheses:

auto s = ("%s is a good number but one needs to know" ~
  " what the question exactly was.").format(42);

Ali


The reason it doesn't work in the second example is because it 
translates to something like this:


auto s = "%s is a good number but one needs to know" ~ ("what the 
question exactly was.".format(42));


The reason encapsulation the two strings works is because you 
append the second string to the first before you call format.


Definitely not a bug.


Orphan format arguments: args[0..1]

2018-12-15 Thread Ali Çehreli via Digitalmars-d-learn

This one confused me until I decided to talk to a rubber ducky:

import std.string;

void main() {
auto s = "%s is a good number".format(42);
}

Fine; it works... Then the string becomes too long and I split it:

auto s = "%s is a good number but one needs to know" ~
 " what the question exactly was.".format(42);

Now there is a compilation error:

  Orphan format arguments: args[0..1]

What? Is that a bug in format? It can't be because the string should be 
concatenated by the compiler as a single string, no? No: operator dot 
has precedence over ~, so format is applied to the second part of the 
string before the concatenation. Doh! This puzzled me a lot.


Anyway, the solution, once again, is to use parentheses:

auto s = ("%s is a good number but one needs to know" ~
  " what the question exactly was.").format(42);

Ali


Re: What can I use to parse a date string in a custom format?

2018-11-27 Thread Neia Neutuladh via Digitalmars-d-learn
On Tue, 27 Nov 2018 18:22:17 +, PacMan wrote:
> I've been looking for a function or library to parse a date in a custom
> format (for validation) similar to C#'s [1]
> TryParseExactbut I couldn't find any. so far I only found
> fromISOExtString(), and fromSimpleString() which doesn't allow me to set
> a custom string format.

http://code.dlang.org/packages/datefmt didn't do it for you?

...that readme is pretty terrible, granted.


What can I use to parse a date string in a custom format?

2018-11-27 Thread PacMan via Digitalmars-d-learn
I've been looking for a function or library to parse a date in a 
custom format (for validation) similar to C#'s [1] 
TryParseExactbut I couldn't find any. so far I only found 
fromISOExtString(), and fromSimpleString() which doesn't allow me 
to set a custom string format. I've looked up at


https://code.dlang.org/search?q=date
https://code.dlang.org/search?q=date+parsing

but to my surpise I didn't find either. Am I using wrong keywords 
or there's no really such method implemented yet?


[1]: 
https://docs.microsoft.com/en-us/dotnet/api/system.datetime.tryparseexact?redirectedfrom=MSDN=netframework-4.7.2#System_DateTime_TryParseExact_System_String_System_String_System_IFormatProvider_System_Globalization_DateTimeStyles_System_DateTime__


Re: expanding variadic into format

2018-10-31 Thread Codifies via Digitalmars-d-learn
On Wednesday, 31 October 2018 at 12:54:52 UTC, Stanislav Blinov 
wrote:

On Wednesday, 31 October 2018 at 12:13:57 UTC, Codifies wrote:

[...]



[...]


As rikki already explained, std.format is a variadic template, 
which gets expanded into argument list at compile time. That's 
why it can't be used with C-syle variadics: when you passed 
_arguments, the expansion treated that as a single argument 
instead of a tuple.
Therefore, to forward arguments to std.format, your 
`printValue` must also be a variadic.


[...]


thanks for this makes it a lot clearer, I'm guessing there is a 
mailing list backing this web forum, as I replied missing some 
other responses.


Re: expanding variadic into format

2018-10-31 Thread Stanislav Blinov via Digitalmars-d-learn

On Wednesday, 31 October 2018 at 12:13:57 UTC, Codifies wrote:
On Wednesday, 31 October 2018 at 12:09:04 UTC, Stanislav Blinov 
wrote:



```
void printValue(Args...)(Font fnt, float x, float y, string 
frmt, auto ref Args args) {

// ...
import std.functional : forward;
string message = format(frmt, forward!args);
// ...
}
```


thats fantastic thanks so much, can you explain a little more 
about whats going on here ?


As rikki already explained, std.format is a variadic template, 
which gets expanded into argument list at compile time. That's 
why it can't be used with C-syle variadics: when you passed 
_arguments, the expansion treated that as a single argument 
instead of a tuple.
Therefore, to forward arguments to std.format, your `printValue` 
must also be a variadic.


There are, broadly speaking, two ways to pass arguments to 
functions: by value and by reference.


When you make a template like this:

void foo(T)(T value) { /* ... */ }

it will take the argument by value, making copies when necessary:

struct S { /* ... */ }

S s;
foo(S.init); // calls foo without copying, argument is 
constructed directly

foo(s); // copies `s` and passes that copy to `foo`

If that template is defined like this:

void foo(T)(ref T value) { /* ... */ }

then it will *only* take argument by reference:

foo(S.init); // error, 'ref' cannot bind to rvalue
foo(s); // no copy is made, `foo` takes `s` by reference

There are more subtleties, especially when taking `const ref` 
arguments, but I won't get into those.


There's a special syntax for template functions: `auto ref` 
arguments. Those are deduced to be by-value or by-reference at 
compile time (see

https://dlang.org/spec/template.html#auto-ref-parameters):

void foo(T)(auto ref T value) { /* ... */ }

foo(S.init); // works, compiles as foo(S);
foo(s); // works, compiles as foo(ref S);

But, because inside of function definition all arguments are 
lvalues, you lose this additional information if you pass them to 
another function directly. To preserve that information, there's 
a `forward` template in std.functional. D doesn't have rvalue 
references, so that template will still copy the bits of 
non-`ref` arguments, but it will not call postblits, etc (it 
`move`s them using std.algorithm.mutation.move).


So, there are two possible ways to implement your print:

// Take all Args by value, i.e. copy everything first time
void printValue(Args...)(Font fnt, float x, float y, string frmt, 
Args args) {
// make copies of every argument in `args` (again) and pass 
those to `format`

auto message = format(frmt, args);
}

or

// Infer whether each argument is an lvalue or not
void printValue(Args...)(Font fnt, float x, float y, string frmt, 
auto ref Args args) {

import std.functional : forward;
// preserve lvalue/rvalue
string message = format(frmt, forward!args);
}

Both allow you to accomplish your goal, but the second one only 
copies the argument bits when necessary.


Getting into finer implementation nuances, conceptually this 
allows a function to even take and pass around non-copyable types 
as arguments. Sadly, this is not widely adopted by Phobos, which 
likes to make unnecessary copies. I.e. the `format` function 
itself takes Args by value, even though it probably should take 
advantage of this specific language feature. But at least calling 
it via `forward`, you only make necessary copies once, instead of 
twice.




Re: expanding variadic into format

2018-10-31 Thread rikki cattermole via Digitalmars-d-learn

On 01/11/2018 1:08 AM, Codifies wrote:

On Wednesday, 31 October 2018 at 11:56:31 UTC, rikki cattermole wrote:

On 01/11/2018 12:53 AM, Codifies wrote:

[...]


Just to confirm, format there is std.format:format right?

Because that isn't using C variadics, its using template variadics.


thought I was using core.vararg and std.format both using templates ??


No. They use different variadics.

Template variadics happen on the template parameter side:

void func(T...)(T args) {
pragma(msg, T.length);
pragma(msg, T.stringof);
}

C variadics happen on the function parameters side and are highly unsafe:

void func(...) {

}

If you use core.vararg you're talking with C.
If you use std.format you're talking D's template variadics.


Re: expanding variadic into format

2018-10-31 Thread Codifies via Digitalmars-d-learn
On Wednesday, 31 October 2018 at 12:09:04 UTC, Stanislav Blinov 
wrote:

On Wednesday, 31 October 2018 at 11:53:52 UTC, Codifies wrote:


void printValue(Font fnt,float x, float y, string frmt, ...)
{
/* matrix math and other stuff removed for readability */
string message = format(frmt, _arguments);




is there some way to somehow transfer my input variadic into 
formats variadic ?



Just to confirm, format there is std.format:format right?
Because that isn't using C variadics, its using template 
variadics.


...as in:

```
void printValue(Args...)(Font fnt, float x, float y, string 
frmt, auto ref Args args) {

// ...
import std.functional : forward;
string message = format(frmt, forward!args);
// ...
}
```


thats fantastic thanks so much, can you explain a little more 
about whats going on here ?


Re: expanding variadic into format

2018-10-31 Thread Codifies via Digitalmars-d-learn
On Wednesday, 31 October 2018 at 11:56:31 UTC, rikki cattermole 
wrote:

On 01/11/2018 12:53 AM, Codifies wrote:

[...]


Just to confirm, format there is std.format:format right?

Because that isn't using C variadics, its using template 
variadics.


thought I was using core.vararg and std.format both using 
templates ??


Re: expanding variadic into format

2018-10-31 Thread Stanislav Blinov via Digitalmars-d-learn

On Wednesday, 31 October 2018 at 11:53:52 UTC, Codifies wrote:


void printValue(Font fnt,float x, float y, string frmt, ...)
{
/* matrix math and other stuff removed for readability */
string message = format(frmt, _arguments);




is there some way to somehow transfer my input variadic into 
formats variadic ?



Just to confirm, format there is std.format:format right?
Because that isn't using C variadics, its using template 
variadics.


...as in:

```
void printValue(Args...)(Font fnt, float x, float y, string frmt, 
auto ref Args args) {

// ...
import std.functional : forward;
string message = format(frmt, forward!args);
// ...
}
```


Re: expanding variadic into format

2018-10-31 Thread rikki cattermole via Digitalmars-d-learn

On 01/11/2018 12:53 AM, Codifies wrote:
I have a routine that was happily printing ASCII strings and values 
using opengl, however I want to improve it so it can be used in the same 
manner as some other languages printf function...


void printValue(Font fnt,float x, float y, string frmt, ...)
{
     /* matrix math and other stuff removed for readability */
     string message = format(frmt, _arguments);


no surprise this naive attempt causes a runtime error as its trying to 
format a range using the first format specifier in frmt


am I going to have to chop frmt into descrete chunks that have just one 
% symbol in them and then build up the formatted message string like that?


is there some way to somehow transfer my input variadic into formats 
variadic ?


Just to confirm, format there is std.format:format right?

Because that isn't using C variadics, its using template variadics.


expanding variadic into format

2018-10-31 Thread Codifies via Digitalmars-d-learn
I have a routine that was happily printing ASCII strings and 
values using opengl, however I want to improve it so it can be 
used in the same manner as some other languages printf function...


void printValue(Font fnt,float x, float y, string frmt, ...)
{
/* matrix math and other stuff removed for readability */
string message = format(frmt, _arguments);


no surprise this naive attempt causes a runtime error as its 
trying to format a range using the first format specifier in frmt


am I going to have to chop frmt into descrete chunks that have 
just one % symbol in them and then build up the formatted message 
string like that?


is there some way to somehow transfer my input variadic into 
formats variadic ?


Re: Dynamic Minimum width with Format / writefln

2018-10-02 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 3 October 2018 at 01:14:24 UTC, Chris Katko wrote:
I'm not sure how I made this mistake. But it seems to only show 
up now if I leave .toStringz() with the writefln.


Yeah.

So what's happening here is toStringz returns the C-style char*, 
which printf works well with, but writef doesn't trust it and 
just prints the pointer value instead of trying to traverse it 
looking for a zero terminator (which might not be there).


Just passing a D string will work consistently.

So maybe I've been staring at code too long tonight and simply 
missed it?


oh probably, it happens to everyone :)


Re: Dynamic Minimum width with Format / writefln

2018-10-02 Thread Chris Katko via Digitalmars-d-learn

On Wednesday, 3 October 2018 at 00:34:33 UTC, Adam D. Ruppe wrote:

On Wednesday, 3 October 2018 at 00:14:03 UTC, Chris Katko wrote:
Except it doesn't work and tries to decode col.width-1 into a 
hexadecimal number and only prints that. ("4D6EF6")


That number certainly isn't col.width (unless you have a width 
of like millions)...


It looks more like a pointer. What is the type of col.name? If 
it is string, this code should work fine.


I'm guessing it is a char*...


I'm not sure how I made this mistake. But it seems to only show 
up now if I leave .toStringz() with the writefln.


writefln("%-*s<", col.width-1, col.name.toStringz() /* here */);

So maybe I've been staring at code too long tonight and simply 
missed it?




Re: Dynamic Minimum width with Format / writefln

2018-10-02 Thread Adam D. Ruppe via Digitalmars-d-learn

On Wednesday, 3 October 2018 at 00:14:03 UTC, Chris Katko wrote:
Except it doesn't work and tries to decode col.width-1 into a 
hexadecimal number and only prints that. ("4D6EF6")


That number certainly isn't col.width (unless you have a width of 
like millions)...


It looks more like a pointer. What is the type of col.name? If it 
is string, this code should work fine.


I'm guessing it is a char*...


Dynamic Minimum width with Format / writefln

2018-10-02 Thread Chris Katko via Digitalmars-d-learn
 - First, I'm confused. The docs say 's' is "whatever it needs to 
be". ("he corresponding argument is formatted in a manner 
consistent with its type:") But what if I specifically want a 
STRING. Because I only see floats, ints, etc. No forced string 
types.


 - Second,

This works fine in D:

printf("%-*s|", col.width-1, col.name.toStringz());

It's a left-aligned, string with a minimum width from the first 
argument, col.width. (-1 because I'm throwing a pipe symbol on 
the end.)


Now with writefln:

writefln("%-*s|", col.width-1, col.name);

Same format specifier, except I don't need a toStringz which is 
nice. Except it doesn't work and tries to decode col.width-1 into 
a hexadecimal number and only prints that. ("4D6EF6")


I looked through the docs:

https://dlang.org/phobos/std_format.html

 '%' Position Flags Width Separator Precision FormatChar

Width:
empty
Integer
'*'

But there are then zero examples or explanations of how to use 
that option. What's going on here?


Re: "%s"-format template function arguments

2018-04-15 Thread ag0aep6g via Digitalmars-d-learn

On 04/15/2018 02:04 PM, vladdeSV wrote:

     foo(1,2,3);

     void foo(T...)(T args)
     {
     writefln("expected: %s", [1,2,3]);
     writefln("actual: %s", args);
     }

The code above will output

     expected: [1, 2, 3]
     actual: 1

How would I go on about to print all the arguments as I expected it, 
using "%s"?


writefln("%s", [args]);

Or avoiding the allocation:

import std.range: only;
writefln("%s", only(args));

You could also change the `args` parameter to give you a stack-based array:

void foo(T)(T[] args ...)
{
writefln("%s", args);
}

[...]

P.S.
I do not understand why only a `1` is printed in the actual result.


This:

writefln("actual: %s", args);

becomes this:

writefln("actual: %s", args[0], args[1], args[2]);

So there are three arguments after the format string. The %s placeholder 
only refers to the first one. The others are ignored.


Re: "%s"-format template function arguments

2018-04-15 Thread Dennis via Digitalmars-d-learn

On Sunday, 15 April 2018 at 12:04:19 UTC, vladdeSV wrote:
How would I go on about to print all the arguments as I 
expected it, using "%s"?


You can expand the template arguments into an array by putting it 
into square brackets: [args]. You can format an array with the 
default notation using %s, for a custom format you can use %( and 
%). See https://dlang.org/phobos/std_format.html


```
void main() {
foo(1, 2, 3);
}

void foo(T...)(T args) {
writefln("%s", [args]); // prints [1, 2, 3]
writefln("%(%s; %)", [args]); //custom ; separator, prints 1; 
2; 3

}
```


Re: "%s"-format template function arguments

2018-04-15 Thread Alex via Digitalmars-d-learn

On Sunday, 15 April 2018 at 12:04:19 UTC, vladdeSV wrote:

Hello people of D-land.

In a template function, I want to format all arguments as if it 
was an array. Se this snippet of code:


foo(1,2,3);

void foo(T...)(T args)
{
writefln("expected: %s", [1,2,3]);
writefln("actual: %s", args);
}

The code above will output

expected: [1, 2, 3]
actual: 1

How would I go on about to print all the arguments as I 
expected it, using "%s"?


Best regards,
Vladimirs Nordholm

---

P.S.
I do not understand why only a `1` is printed in the actual 
result.


If you define foo like that, then, you advice D to handle the 
input as separate objects. That's ok. But then, you have to 
define that you still assume, they belong together, like an array.


In this example the solution is simple:

´´´
void main()
{
foo(1,2,3);
}

void foo(T...)(T args)
{
import std.stdio : writefln;
import std.range : only;

writefln("expected: %s", [1,2,3]);
writefln("actual: %s", args.only);
}
´´´
However, there will be problems, if the types of elements 
differs: While the template foo will be able to handle this, the 
std.range : only function won't. It assumes at least something 
common across them.


https://dlang.org/library/std/range/only.html



"%s"-format template function arguments

2018-04-15 Thread vladdeSV via Digitalmars-d-learn

Hello people of D-land.

In a template function, I want to format all arguments as if it 
was an array. Se this snippet of code:


foo(1,2,3);

void foo(T...)(T args)
{
writefln("expected: %s", [1,2,3]);
writefln("actual: %s", args);
}

The code above will output

expected: [1, 2, 3]
actual: 1

How would I go on about to print all the arguments as I expected 
it, using "%s"?


Best regards,
Vladimirs Nordholm

---

P.S.
I do not understand why only a `1` is printed in the actual 
result.


Re: compile-time checked format strings

2018-01-13 Thread kdevel via Digitalmars-d-learn

On Saturday, 13 January 2018 at 19:40:09 UTC, Adam D. Ruppe wrote:
For ints, it catches all that, but for float, it just bails out 
of the check as soon as it actually *succeeds* - because that 
kills CTFE.


Confirmed. Thanks!

args.d
```
import std.stdio;

void main ()
{
//   writefln!"%2.2d %2.2d" (1); // OK: Orphan format specifier
//writefln!"%2.2d" (1, 2); // OK: Orphan format arguments:
}
```



Re: compile-time checked format strings

2018-01-13 Thread Basile B. via Digitalmars-d-learn

On Saturday, 13 January 2018 at 19:40:09 UTC, Adam D. Ruppe wrote:

On Saturday, 13 January 2018 at 19:15:49 UTC, kdevel wrote:

dmd checks the types but does not count the arguments.


so note that dmd doesn't actually do any checks here - it is 
all done in library code.


The implementation is amusingly simple:
https://github.com/dlang/phobos/blob/master/std/format.d#L5803


Yeah there's already an interesting issue about this

https://issues.dlang.org/show_bug.cgi?id=17381




Re: compile-time checked format strings

2018-01-13 Thread Adam D. Ruppe via Digitalmars-d-learn

On Saturday, 13 January 2018 at 19:15:49 UTC, kdevel wrote:

dmd checks the types but does not count the arguments.


so note that dmd doesn't actually do any checks here - it is all 
done in library code.


The implementation is amusingly simple:
https://github.com/dlang/phobos/blob/master/std/format.d#L5803

try format(...) catch(Exception) trigger compile time error.


But, since float format doesn't work at compile time, this method 
doesn't actually work to catch any float-related problems except 
mismatching format characters!


For ints, it catches all that, but for float, it just bails out 
of the check as soon as it actually *succeeds* - because that 
kills CTFE.


We should just put in a fake stub float printer to hack this past 
for checking purposes.


compile-time checked format strings

2018-01-13 Thread kdevel via Digitalmars-d-learn
occasion: 
http://forum.dlang.org/thread/mutegviphsjwqzqfo...@forum.dlang.org?page=3#post-mailman.2136.1515709204.9493.digitalmars-d-announce:40puremagic.com


dmd checks the types but does not count the arguments.

ctcfs.d
```
import std.stdio;
import std.math;

void unit (T) ()
{
   auto pi = 4 * atan (T (1));
   writefln!"%30.24f" (pi, pi); // no error!
   writefln!"%30.24f %30.24f" (pi, pi); // OK
//   writefln!"%30.24d %30.24f" (pi, pi); // OK:  "incompatible 
format character

   writefln!"%30.24f %30.24f %30.24f" (pi, pi); // no error
}

void main ()
{
   unit!float;
}
```

$ dmd ctcfs.d
$ ./ctcfs
3.141592741012573242187500
3.141592741012573242187500 3.141592741012573242187500
3.141592741012573242187500 3.141592741012573242187500

std.format.FormatException@/.../dmd2/linux/bin64/../../src/phobos/std/format.d(479):
 Orphan format specifier: %f

/.../dmd2/linux/bin64/../../src/phobos/std/exception.d:615 pure 
@safe bool 
std.exception.enforceEx!(std.format.FormatException).enforceEx!(bool).enforceEx(bool, lazy immutable(char)[], immutable(char)[], ulong) [0x44503c]
/.../dmd2/linux/bin64/../../src/phobos/std/format.d:479 @safe 
uint std.format.formattedWrite!(std.stdio.File.LockingTextWriter, 
char, float, float).formattedWrite(ref 
std.stdio.File.LockingTextWriter, const(char[]), float, float) 
[0x44c436]
/.../dmd2/linux/bin64/../../src/phobos/std/stdio.d:1496 @safe 
void std.stdio.File.writefln!(char, float, 
float).writefln(const(char[]), float, float) [0x44c340]
/.../dmd2/linux/bin64/../../src/phobos/std/stdio.d:3797 @safe 
void std.stdio.writefln!(char, float, 
float).writefln(const(char[]), float, float) [0x44c2b7]
/.../dmd2/linux/bin64/../../src/phobos/std/stdio.d:3791 @safe 
void std.stdio.writefln!("%30.24f %30.24f %30.24f", float, 
float).writefln(float, float) [0x44da27]

ctcfs.d:10 @safe void ctcfs.unit!(float).unit() [0x443673]
ctcfs.d:15 _Dmain [0x443614]




Re: Problem with std.string.strip(): Can't use result in format routine

2017-09-05 Thread Ky-Anh Huynh via Digitalmars-d-learn
On Tuesday, 5 September 2017 at 13:56:20 UTC, Andrea Fontana 
wrote:
I used `lines(stdin)` as in 
https://dlang.org/phobos/std_stdio.html#.lines . My source 
code is here


https://github.com/icy/dusybox/blob/master/src/plotbar/main.d#L47 .

Thanks for your support.


I think formattedRead is consuming your string.


Great catch. You're right. Thanks a lot.

`strip()` returns a slice. I use another reference variable to 
save them and it's fine now.


[code]
auto line_st = line.strip();
auto line_st2 = line_st;


writefln("Stripped lined is %s", line_st2); // works fine
[/code]



Re: Problem with std.string.strip(): Can't use result in format routine

2017-09-05 Thread Andrea Fontana via Digitalmars-d-learn

On Tuesday, 5 September 2017 at 13:40:18 UTC, Ky-Anh Huynh wrote:

On Tuesday, 5 September 2017 at 13:17:34 UTC, Azi Hassan wrote:


Maybe it has something to do with how you read from STDIN. Can 
you show that part of the code to see if I can reproduce the 
issue ?


I used `lines(stdin)` as in 
https://dlang.org/phobos/std_stdio.html#.lines . My source code 
is here


https://github.com/icy/dusybox/blob/master/src/plotbar/main.d#L47 .

Thanks for your support.


I think formattedRead is consuming your string.

Andrea


Re: Problem with std.string.strip(): Can't use result in format routine

2017-09-05 Thread Ky-Anh Huynh via Digitalmars-d-learn

On Tuesday, 5 September 2017 at 13:17:34 UTC, Azi Hassan wrote:


Maybe it has something to do with how you read from STDIN. Can 
you show that part of the code to see if I can reproduce the 
issue ?


I used `lines(stdin)` as in 
https://dlang.org/phobos/std_stdio.html#.lines . My source code 
is here


https://github.com/icy/dusybox/blob/master/src/plotbar/main.d#L47 
.


Thanks for your support.


Re: Problem with std.string.strip(): Can't use result in format routine

2017-09-05 Thread Azi Hassan via Digitalmars-d-learn

On Tuesday, 5 September 2017 at 12:38:54 UTC, Ky-Anh Huynh wrote:

Hi,

I read line from STDIN , and strip them

[code]
auto line_st = line.strip();
[/code]


However, I can't use result in another format routine. Assume 
my input line is "foobar":


[code]
writeln("Stripped line is %s", line_st);
[/code]

This code only prints "Stripped line is ". If I use instead

[code]
writeln("Stripped line is %s", line.strip());
[/code]

the result is "Stripped line is foobar".

What're the difference between the two uses? I'm using `dmd 
v2.075.1`.


Thanks a lot.


Maybe it has something to do with how you read from STDIN. Can 
you show that part of the code to see if I can reproduce the 
issue ?


Problem with std.string.strip(): Can't use result in format routine

2017-09-05 Thread Ky-Anh Huynh via Digitalmars-d-learn

Hi,

I read line from STDIN , and strip them

[code]
auto line_st = line.strip();
[/code]


However, I can't use result in another format routine. Assume my 
input line is "foobar":


[code]
writeln("Stripped line is %s", line_st);
[/code]

This code only prints "Stripped line is ". If I use instead

[code]
writeln("Stripped line is %s", line.strip());
[/code]

the result is "Stripped line is foobar".

What're the difference between the two uses? I'm using `dmd 
v2.075.1`.


Thanks a lot.


Re: Multi dimensional array format priting

2017-08-25 Thread Vino.B via Digitalmars-d-learn

On Friday, 25 August 2017 at 17:41:31 UTC, Vino.B wrote:
On Friday, 25 August 2017 at 17:02:53 UTC, Jonathan M Davis 
wrote:
On Friday, August 25, 2017 16:45:16 Vino.B via 
Digitalmars-d-learn wrote:

Hi,

  Request your help on the below issue,

Issue : While appending data to a array the data is getting 
duplicated.


Program:
import std.file: dirEntries, isFile, SpanMode;
import std.stdio: writeln, writefln;
import std.algorithm: filter, map;
import std.array: array;
import std.typecons: tuple;

string[] Subdata;
void main ()
{
  auto dFiles = dirEntries("C:\\Temp\\TEAM",
SpanMode.shallow).filter!(a => a.isFile).map!(a => 
tuple(a.name ,

a.timeCreated)).array;
  foreach (d; dFiles)
   {
Subdata ~= d[0];
Subdata ~= d[1].toSimpleString;
writeln(Subdata);
}
}

Output:

["C:\\Temp\\TEAM\\test1.pdf", "2017-Aug-24 18:23:00.8946851"]
 -
duplicate line
["C:\\Temp\\TEAM\\test1.pdf", "2017-Aug-24 18:23:00.8946851",
"C:\\TempTEAM\\test5.xlsx", "2017-Aug-25 23:38:14.486421"]


You keep printing out the entire array on every iteration of 
the loop, so of course, you're going to see stuff output 
multiple times. If you did something like


import std.stdio;

void main()
{
int[] arr;
foreach(i; 0 .. 5)
{
arr ~= i * 10;
writeln(arr);
}
}

then you'd get the output

[0]
[0, 10]
[0, 10, 20]
[0, 10, 20, 30]
[0, 10, 20, 30, 40]

whereas if you did

import std.stdio;

void main()
{
int[] arr;
foreach(i; 0 .. 5)
arr ~= i * 10;
writeln(arr);
}

then you'd just get

[0, 10, 20, 30, 40]

- Jonathan M Davis


Hi All,

 Thank you very much, that was my mistake. The main idea is to 
implement parallelism and now i get only single data as there 
are 2 files in each of the folders, but it is listing only 1 
per folder.


import std.file: dirEntries, isFile, SpanMode;
import std.stdio: writeln, writefln;
import std.algorithm: filter, map;
import std.array: array;
import std.typecons: tuple;
import std.parallelism;
string[] Subdata;

auto Dirlst = [ "C:\\Temp\\TEAM", "C:\\Temp\\PROD_TEAM"];

string[] Test (string Fs)
{
auto dFiles = dirEntries(Fs, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;

foreach (d; dFiles)
 {  Subdata ~=  d[0]; Subdata ~= d[1].toSimpleString; }
return Subdata;

}
void main ()
{
 foreach (string Fs; Dirlst[0 .. $])
{
   auto TestTask = task(, Fs);
TestTask.executeInNewThread();
auto TestTaskData = TestTask.yieldForce;
writefln("%-63s %.20s", TestTaskData[0], TestTaskData[1]);
}
}

Output:

C:\Temp\TEAM\Test.pdf
2017-Aug-24 18:23:00
C:\Temp\\PROD_TEAM\DND1.pdf  
2017-Aug-25 23:38:04



The folder C:\Temp\TEAM contains 2 files and folder 
C:\Temp\\PROD_TEAM contain 4 files but it display only 1 file 
per folder.


Hi,

 I was able to find the solution, thank you very much, please let 
me know if there are any good logic than below,


import std.file: dirEntries, isFile, SpanMode;
import std.stdio: writeln, writefln;
import std.algorithm: filter, map;
import std.array: array;
import std.typecons: tuple;
import std.parallelism;
string[][] Subdata;

auto Dirlst = [ "C:\\Temp\\TEAM" ];

string[][] CleanFiles (string Fs)
{
auto dFiles = dirEntries(Fs, SpanMode.shallow).filter!(a => 
a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;

foreach (d; dFiles)
{
 Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
}
return Subdata;

}
void main ()
{
 foreach (string Fs; Dirlst[0 .. $])
{
auto MCleanTask = task(, Fs);
MCleanTask.executeInNewThread();
auto MCleanTaskData = MCleanTask.yieldForce;
foreach(i; MCleanTaskData[0 .. $])
writefln("%-(%-63s %)", i);
}

}



From,
Vino.B




Re: wth!! ctfe cannot format floating point at compile time?

2017-08-14 Thread Adam D. Ruppe via Digitalmars-d-learn
On Monday, 14 August 2017 at 13:11:20 UTC, Steven Schveighoffer 
wrote:
Another reasonable idea is to have the compiler call the 
function for you.


Yeah, I was thinking that too. Heck, the compiler prolly uses it 
for reading source and writing errors.


Perhaps just special case hack the function it uses in the ctfe 
engine.


Re: wth!! ctfe cannot format floating point at compile time?

2017-08-14 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/13/17 11:44 PM, Adam D. Ruppe wrote:

On Monday, 14 August 2017 at 01:52:16 UTC, Johnson Jones wrote:
pretty simply, trying to convert a floating point to a string in a 
ctfe function and it thinks that it is too complex to do in a ctfe, 
really?


It uses a C function to do the conversion, which is not available at 
compile time since CTFE can't run extern functions.


And it is a LOT harder to do than you might think which is why it still 
uses the C function - implementing one in D is legit pretty complex.


When I first saw this too, I figured it must be simple to slap something 
together, even if it is horribly inefficient and doesn't work 
everywhere... and I ended up giving up after spending a few hours on it 
too. If you search the web, you get academic papers describing it, eeek.


Of course, it might be reasonable to port one of the C implementations 
directly... but even that is a fairly big job (and you might as well 
just call the C function itself for all but CTFE cases) - the GNU one is 
1,300 lines, for example. And that has an incompatible license with the 
rest of druntime!


Another reasonable idea is to have the compiler call the function for 
you. Since the compiler only ever exists on platforms with libc 
available, the libc function is available to the compiler too. having 
some special intrinsic to format floating points wouldn't be out of the 
question.


It is quite limiting to have floating point string conversions not 
available.


-Steve


Re: wth!! ctfe cannot format floating point at compile time?

2017-08-14 Thread Stefan Koch via Digitalmars-d-learn

On Monday, 14 August 2017 at 04:29:17 UTC, Johnson wrote:


```
auto valueToString(alias v)(){return v.stringof;}
enum a = valueToString!(0.75);
static assert(a == "0.75");
```
Thanks! You'd think that to would do this internally 
automatically ;/


It only works on literals.
valueToString!(a) will give you a;


Re: wth!! ctfe cannot format floating point at compile time?

2017-08-13 Thread Johnson via Digitalmars-d-learn

On Monday, 14 August 2017 at 03:52:40 UTC, HypperParrow wrote:

On Monday, 14 August 2017 at 01:52:16 UTC, Johnson Jones wrote:
Error: uncaught CTFE exception 
std.format.FormatException("Cannot format floating point types 
at compile-time")

called from here: to(0.75)

pretty simply, trying to convert a floating point to a string 
in a ctfe function and it thinks that it is too complex to do 
in a ctfe, really?


There is this trick as workaround:

```
auto valueToString(alias v)(){return v.stringof;}
enum a = valueToString!(0.75);
static assert(a == "0.75");
```

The problem with to() and format() is that is requires external 
library calls that are not available at compile-time, just like 
when you try to use malloc() at CT.


Thanks! You'd think that to would do this internally 
automatically ;/


Re: wth!! ctfe cannot format floating point at compile time?

2017-08-13 Thread Johnson via Digitalmars-d-learn

On Monday, 14 August 2017 at 03:44:27 UTC, Adam D. Ruppe wrote:

On Monday, 14 August 2017 at 01:52:16 UTC, Johnson Jones wrote:
pretty simply, trying to convert a floating point to a string 
in a ctfe function and it thinks that it is too complex to do 
in a ctfe, really?


It uses a C function to do the conversion, which is not 
available at compile time since CTFE can't run extern functions.


And it is a LOT harder to do than you might think which is why 
it still uses the C function - implementing one in D is legit 
pretty complex.


When I first saw this too, I figured it must be simple to slap 
something together, even if it is horribly inefficient and 
doesn't work everywhere... and I ended up giving up after 
spending a few hours on it too. If you search the web, you get 
academic papers describing it, eeek.


Of course, it might be reasonable to port one of the C 
implementations directly... but even that is a fairly big job 
(and you might as well just call the C function itself for all 
but CTFE cases) - the GNU one is 1,300 lines, for example. And 
that has an incompatible license with the rest of druntime!


I don't believe that! Just run dmd on it like ctfe does. Maybe 
the problem is that to should be a bit more intelligent and if it 
is ran at ctfe should try something else.


I don't see why it would be all that difficult maybe for complex 
floating point numbers it is but for most ordinary numbers used 
as most literals(e.g., terminating decimals), it is very very 
easy to convert. It is not much more difficult than storing it as 
a string.


After all 0.43425 is actually a string that will be converted to 
fp. I realize there are accuracy issues but I seriously think 
there should be something suitable rather than crapping out ;/ 
e.g., 0.43425 = 43425/10.


Remember, we are converting a literal string represented floating 
point value to a string. It should be a direct conversion. 
Basically a copy and paste. (at run time, it is different, 
obviously since the value is actually a fp, but at compile time 
it is simply a string.

There is this trick as workaround:

As HypperParrow says, it's easy as pi:

```
auto valueToString(alias v)(){return v.stringof;}
enum a = valueToString!(0.75);
static assert(a == "0.75");
```


to!string(float) should detect that and just do it. No need to 
try to do anything complex.





Re: wth!! ctfe cannot format floating point at compile time?

2017-08-13 Thread HypperParrow via Digitalmars-d-learn

On Monday, 14 August 2017 at 01:52:16 UTC, Johnson Jones wrote:
Error: uncaught CTFE exception 
std.format.FormatException("Cannot format floating point types 
at compile-time")

called from here: to(0.75)

pretty simply, trying to convert a floating point to a string 
in a ctfe function and it thinks that it is too complex to do 
in a ctfe, really?


There is this trick as workaround:

```
auto valueToString(alias v)(){return v.stringof;}
enum a = valueToString!(0.75);
static assert(a == "0.75");
```

The problem with to() and format() is that is requires external 
library calls that are not available at compile-time, just like 
when you try to use malloc() at CT.


Re: wth!! ctfe cannot format floating point at compile time?

2017-08-13 Thread Adam D. Ruppe via Digitalmars-d-learn

On Monday, 14 August 2017 at 01:52:16 UTC, Johnson Jones wrote:
pretty simply, trying to convert a floating point to a string 
in a ctfe function and it thinks that it is too complex to do 
in a ctfe, really?


It uses a C function to do the conversion, which is not available 
at compile time since CTFE can't run extern functions.


And it is a LOT harder to do than you might think which is why it 
still uses the C function - implementing one in D is legit pretty 
complex.


When I first saw this too, I figured it must be simple to slap 
something together, even if it is horribly inefficient and 
doesn't work everywhere... and I ended up giving up after 
spending a few hours on it too. If you search the web, you get 
academic papers describing it, eeek.


Of course, it might be reasonable to port one of the C 
implementations directly... but even that is a fairly big job 
(and you might as well just call the C function itself for all 
but CTFE cases) - the GNU one is 1,300 lines, for example. And 
that has an incompatible license with the rest of druntime!


wth!! ctfe cannot format floating point at compile time?

2017-08-13 Thread Johnson Jones via Digitalmars-d-learn
Error: uncaught CTFE exception std.format.FormatException("Cannot 
format floating point types at compile-time")

called from here: to(0.75)

pretty simply, trying to convert a floating point to a string in 
a ctfe function and it thinks that it is too complex to do in a 
ctfe, really?


Re: Format g bug ?

2017-08-09 Thread Steven Schveighoffer via Digitalmars-d-learn

On 8/9/17 4:10 PM, Temtaime wrote:

Sorry, messed up numbers

Expected:

3.11
3.11
3.1
3.1

Seems g outputs one digit less


I was bugged by this too.

It's not a bug. For the %f specifier, the number represents the number 
of digits *after* the decimal.


For the %g specifier, the number represents the number of digits *before 
and after* the decimal.


So:

writefln(`%.1g`, 31.11) -> 31

The most annoying thing is that %g is the default specifier for floating 
point.


-Steve


Re: Format g bug ?

2017-08-09 Thread Temtaime via Digitalmars-d-learn

Sorry, messed up numbers

Expected:

3.11
3.11
3.1
3.1

Seems g outputs one digit less


Format g bug ?

2017-08-09 Thread Temtaime via Digitalmars-d-learn

import std.stdio;

void main()
{
writefln(`%.2g`, 3.11);
writefln(`%.2f`, 3.11);

writefln(`%.1g`, 3.11);
writefln(`%.1f`, 3.11);
}


3.1
3.11
3
3.1

But expected

3.1
3.11
3.1
3.11


Re: Stack Trace format

2017-04-30 Thread Szabo Bogdan via Digitalmars-d-learn

On Sunday, 30 April 2017 at 20:31:09 UTC, Szabo Bogdan wrote:

Hi,

I noticed that on different platforms the 
`object.Throwable.TraceInfo` has different formats. A program 
compiled on osx with ldc2 has all the TraceInfo empty... Why?


I want to parse those strings or somehow iterate trough all the 
stack elements, but if I get a different format on different 
platforms it's not that easy to determine at what position in 
the string is the address or the function name.


I would appreciate if anyone have an idea of how I can do this 
without a big headache...


Thanks!


Actually I found the `defaultTraceHandler` here:

https://github.com/dlang/druntime/blob/7caaf7cbb699a2a1944b2ac087c3b07d23db6802/src/core/runtime.d#L534


Stack Trace format

2017-04-30 Thread Szabo Bogdan via Digitalmars-d-learn

Hi,

I noticed that on different platforms the 
`object.Throwable.TraceInfo` has different formats. A program 
compiled on osx with ldc2 has all the TraceInfo empty... Why?


I want to parse those strings or somehow iterate trough all the 
stack elements, but if I get a different format on different 
platforms it's not that easy to determine at what position in the 
string is the address or the function name.


I would appreciate if anyone have an idea of how I can do this 
without a big headache...


Thanks!


Re: How to fix date format?

2017-04-26 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, April 26, 2017 06:55:01 Suliman via Digitalmars-d-learn wrote:
> Thanks! That's work!
>
> But why I can't do it in single line like:
> string dt =
> DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!string))
> ; "Error: function std.datetime.DateTime.toISOExtString () const is not
> callable using argument types (DateTime)"
>
> And should do:
>
> DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
> string str = dt.toISOExtString();

You can do it in a single line. I just split it up so that it fit better in
the limited line length of an e-mail, and because it was clearer with the
types. The problem is that you're calling toISOExtString incorrectly. The
from*String functions are static functions, but the to*String functions are
normal member functions. You don't pass a DateTime to toISOExtString. You
just call it on the DateTime object. So, if you want one line, you end up
with something like

auto str = DateTime.fromSimpleString(point[1].coerce!string).
   toISOExtString().replace("T", " ");

- Jonathan M Davis



Re: How to fix date format?

2017-04-26 Thread Suliman via Digitalmars-d-learn
On Wednesday, 26 April 2017 at 05:21:32 UTC, Jonathan M Davis 
wrote:
On Wednesday, April 26, 2017 04:02:12 Suliman via 
Digitalmars-d-learn wrote:

I tried to do:

writeln(DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!
string)));

But got error:

Error: function std.datetime.DateTime.toISOExtString () const 
is

not callable using argument types (DateTime)
Error: function database.Database.getSingleTrackInfo no return
exp; or assert(0); at end of function


toISOExtString is a normal member function on DateTime, not a 
static member function. If point[1].coerce!string is giving you 
a string in Boost's "simple time" format (e.g. "2016-Jan-04 
12:19:17"), then 
DateTime.fromSimpleString(point[1].coerce!string) will give you 
a DateTime. Then if you called toISOExtString() on that, e.g.


DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
string str = dt.toISOExtString();

then the string would be in the ISO extended format (e.g. 
"2016-01-04T12:19:17"). If you then wanted that in the format 
"2016-01-04 12:19:17", then you could just replace the 'T' with 
' ', e.g.


DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
string str = dt.toISOExtString().replace("T", " ");

And if you have "2016-01-04 12:19:17", and you want to convert 
that to the Boost simple time format, you could do


DateTime dt = DateTime.fromISOExtString(str.replace(" ", "T"))
auto simpleStr = dt.toSimpleString();

Hopefully, that helps.

- Jonathan M Davis


Thanks! That's work!

But why I can't do it in single line like:
string dt = 
DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!string));
"Error: function std.datetime.DateTime.toISOExtString () const is 
not callable using argument types (DateTime)"


And should do:

DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
string str = dt.toISOExtString();


Re: How to fix date format?

2017-04-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Wednesday, April 26, 2017 04:02:12 Suliman via Digitalmars-d-learn wrote:
> I tried to do:
>
> writeln(DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!
> string)));
>
> But got error:
>
> Error: function std.datetime.DateTime.toISOExtString () const is
> not callable using argument types (DateTime)
> Error: function database.Database.getSingleTrackInfo no return
> exp; or assert(0); at end of function

toISOExtString is a normal member function on DateTime, not a static member
function. If point[1].coerce!string is giving you a string in Boost's
"simple time" format (e.g. "2016-Jan-04 12:19:17"), then
DateTime.fromSimpleString(point[1].coerce!string) will give you a DateTime.
Then if you called toISOExtString() on that, e.g.

DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
string str = dt.toISOExtString();

then the string would be in the ISO extended format (e.g.
"2016-01-04T12:19:17"). If you then wanted that in the format
"2016-01-04 12:19:17", then you could just replace the 'T' with ' ', e.g.

DateTime dt = DateTime.fromSimpleString(point[1].coerce!string);
string str = dt.toISOExtString().replace("T", " ");

And if you have "2016-01-04 12:19:17", and you want to convert that to the
Boost simple time format, you could do

DateTime dt = DateTime.fromISOExtString(str.replace(" ", "T"))
auto simpleStr = dt.toSimpleString();

Hopefully, that helps.

- Jonathan M Davis



Re: How to fix date format?

2017-04-25 Thread Ali Çehreli via Digitalmars-d-learn

On 04/25/2017 10:41 AM, Suliman wrote:

I am using mysql native. Date in DB have next format: 2016-11-01 06:19:37

But every tile when I am trying to get it I am getting such format:
2016-Oct-31 15:37:24

I use next code:
writeln(point[1].coerce!string);

Why coerce is forcing format changing? How I can extract result as
without month name between digits or easily convert it in proper data
format?


Here's a silly little code that gets the job done. :)

import std.stdio;

string[string] numeralMonth;

static this() {
numeralMonth = [ "Jan" : "01", "Feb" : "02", "Mar" : "03", "Apr" : 
"04",
 "May" : "05", "Jun" : "06", "Jul" : "07", "Aug" : 
"08",
 "Sep" : "09", "Oct" : "10", "Nov" : "11", "Dec" : 
"12" ];

}

auto goodDateFormat(string str) {
import std.range : chain, choose;
if (str.length >= 8) {
auto key = str[5..8];
auto numeral = key in numeralMonth;
if (numeral) {
import std.string : format;
return format("%s%s%s", str[0..5], *numeral, str[8..$]);
}
}
return str;
}

void main() {
writeln(goodDateFormat("2016-Oct-31 15:37:24"));
}

Ali



Re: How to fix date format?

2017-04-25 Thread Suliman via Digitalmars-d-learn

I tried to do:

writeln(DateTime.toISOExtString(DateTime.fromSimpleString(point[1].coerce!string)));

But got error:

Error: function std.datetime.DateTime.toISOExtString () const is 
not callable using argument types (DateTime)
Error: function database.Database.getSingleTrackInfo no return 
exp; or assert(0); at end of function


Re: How to fix date format?

2017-04-25 Thread Suliman via Digitalmars-d-learn

On Tuesday, 25 April 2017 at 20:10:02 UTC, Jonathan M Davis wrote:
On Tuesday, April 25, 2017 17:41:25 Suliman via 
Digitalmars-d-learn wrote:
I am using mysql native. Date in DB have next format: 
2016-11-01 06:19:37


But every tile when I am trying to get it I am getting such
format:
2016-Oct-31 15:37:24

I use next code:
writeln(point[1].coerce!string);

Why coerce is forcing format changing? How I can extract 
result as without month name between digits or easily convert 
it in proper data format?


What types are dealing with here? What is point[1]?

- Jonathan M Davis


writeln(point[1].coerce!string);
writeln(point[1].type);


std.datetime.DateTime
std.variant.VariantN!20LU.VariantN


Re: How to fix date format?

2017-04-25 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, April 25, 2017 17:41:25 Suliman via Digitalmars-d-learn wrote:
> I am using mysql native. Date in DB have next format: 2016-11-01
> 06:19:37
>
> But every tile when I am trying to get it I am getting such
> format:
> 2016-Oct-31 15:37:24
>
> I use next code:
> writeln(point[1].coerce!string);
>
> Why coerce is forcing format changing? How I can extract result
> as without month name between digits or easily convert it in
> proper data format?

What types are dealing with here? What is point[1]?

- Jonathan M Davis



How to fix date format?

2017-04-25 Thread Suliman via Digitalmars-d-learn
I am using mysql native. Date in DB have next format: 2016-11-01 
06:19:37


But every tile when I am trying to get it I am getting such 
format:

2016-Oct-31 15:37:24

I use next code:
writeln(point[1].coerce!string);

Why coerce is forcing format changing? How I can extract result 
as without month name between digits or easily convert it in 
proper data format?


Re: Difference between dstring and string format specifiers support. Bug?

2016-11-09 Thread Ali Çehreli via Digitalmars-d-learn

On 11/09/2016 01:20 AM, Ali Çehreli wrote:

> arrayPtrDiff() is at the bottom of the same file but works correctly
> only for char strings:
>
>   https://github.com/dlang/phobos/blob/master/std/format.d#L6573

What I meant is, using arrayPtrDiff() is correct only for char strings. 
Otherwise, arrayPtrDiff() is working properly.


Ali



Re: Difference between dstring and string format specifiers support. Bug?

2016-11-09 Thread Ali Çehreli via Digitalmars-d-learn

On 11/09/2016 12:21 AM, Vadim Lopatin wrote:

Looks like bug.
dchar[] and wchar[] format strings support less specifiers than char[]

import std.format;
string test1 = "%02d".format(1); // works
assert(test1 == "01");
dstring test2 = "%d"d.format(1); // works
assert(test2 == "1"d);
wstring test3 = "%02d"w.format(1); // fails
assert(test3 == "01"w);
dstring test4 = "%02d"d.format(1); // fails
assert(test4 == "01"d);



It's a bug that std.format uses arrayPtrDiff() here:

  https://github.com/dlang/phobos/blob/master/std/format.d#L993

arrayPtrDiff() is at the bottom of the same file but works correctly 
only for char strings:


  https://github.com/dlang/phobos/blob/master/std/format.d#L6573

Please report it.

Ali



Difference between dstring and string format specifiers support. Bug?

2016-11-09 Thread Vadim Lopatin via Digitalmars-d-learn

Looks like bug.
dchar[] and wchar[] format strings support less specifiers than 
char[]


import std.format;
string test1 = "%02d".format(1); // works
assert(test1 == "01");
dstring test2 = "%d"d.format(1); // works
assert(test2 == "1"d);
wstring test3 = "%02d"w.format(1); // fails
assert(test3 == "01"w);
dstring test4 = "%02d"d.format(1); // fails
assert(test4 == "01"d);



Re: Difference between dstring and string format specifiers support. Bug?

2016-11-09 Thread Vadim Lopatin via Digitalmars-d-learn
On Wednesday, 9 November 2016 at 08:21:53 UTC, Vadim Lopatin 
wrote:

Looks like bug.
dchar[] and wchar[] format strings support less specifiers than 
char[]


import std.format;
string test1 = "%02d".format(1); // works
assert(test1 == "01");
dstring test2 = "%d"d.format(1); // works
assert(test2 == "1"d);
wstring test3 = "%02d"w.format(1); // fails
assert(test3 == "01"w);
dstring test4 = "%02d"d.format(1); // fails
assert(test4 == "01"d);


dmd 2.072.0



Re: How to use `format` to repeat a character

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/11/2016 03:02 PM, Mike Parker wrote:
> You can do it in D with custom format specifiers. See:
> 
> https://wiki.dlang.org/Defining_custom_print_format_specifiers

Thanks for the pointer.  I'll keep that in mind.

-- 
Bahman


Re: How to use `format` to repeat a character

2016-07-11 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Jul 11, 2016 at 02:53:24PM +0430, Bahman Movaqar via 
Digitalmars-d-learn wrote:
> On 07/11/2016 02:44 PM, ketmar wrote:
[...]
> > the fact that format can insert spaces. it is like: "ok, it can do
> > spaces. i bet there should be some way to use any character instead
> > of space. after all, the implementation would be the same, right?!"
> > and then... oops.
> 
> That's my story.
> 
> Thanks people for your help.

Here's a cheating way of doing it:

import std.stdio, std.range;
writefln("%.5s", repeat('-'));

It's cheating because the actual repeat is created by repeat(), but we
(ab)use the fact that the precision flag is treated as "maximum number
of characters" in the %s specifier to be able to control the length of
the repeat from the format string.


T

-- 
INTEL = Only half of "intelligence".


Re: How to use `format` to repeat a character

2016-07-11 Thread Meta via Digitalmars-d-learn

On Monday, 11 July 2016 at 09:02:12 UTC, Bahman Movaqar wrote:
I'm sure I'm missing something very simple but how can I create 
a string

like "" using `format`?
I check the docs on `format` and tried many variations including
`format("%.*c\n", 4, '-')` but got nowhere.

I'd appreciate any hint/help on this.


There's at least one way to do this, by using position arguments 
and repeating it the desired number of times in the format string.


format("%1$s%1$s%1$s%1$s\n", '-')

But as you can see this is pretty ugly and can easily introduce 
bugs in your format string.


Re: How to use `format` to repeat a character

2016-07-11 Thread Mike Parker via Digitalmars-d-learn

On Monday, 11 July 2016 at 10:23:24 UTC, Bahman Movaqar wrote:

On 07/11/2016 02:44 PM, ketmar wrote:

On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote:

What makes you expect that format should have that feature? :)


I somehow recalled I could do that in C and then there was the 
"minimum field width" in the docs, so I thought it's possible 
I'm just not understanding the docs clearly :-)


You can do it in D with custom format specifiers. See:

https://wiki.dlang.org/Defining_custom_print_format_specifiers


Re: How to use `format` to repeat a character

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
On 07/11/2016 02:44 PM, ketmar wrote:
> On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote:
>> What makes you expect that format should have that feature? :)

I somehow recalled I could do that in C and then there was the "minimum
field width" in the docs, so I thought it's possible I'm just not
understanding the docs clearly :-)

> the fact that format can insert spaces. it is like: "ok, it can do
> spaces. i bet there should be some way to use any character instead of
> space. after all, the implementation would be the same, right?!" and
> then... oops.

That's my story.

Thanks people for your help.




Re: How to use `format` to repeat a character

2016-07-11 Thread ketmar via Digitalmars-d-learn

On Monday, 11 July 2016 at 09:31:49 UTC, Ali Çehreli wrote:

What makes you expect that format should have that feature? :)


the fact that format can insert spaces. it is like: "ok, it can 
do spaces. i bet there should be some way to use any character 
instead of space. after all, the implementation would be the 
same, right?!" and then... oops.


probably this worth a ER.


Re: How to use `format` to repeat a character

2016-07-11 Thread ag0aep6g via Digitalmars-d-learn

On 07/11/2016 11:31 AM, Ali Çehreli wrote:

 // Another one that combines multiple range algorithms
 import std.range : iota;
 import std.algorithm : map;
 assert(7.iota.map!(i => i % 2 ? '=' : '-').equal("-=-=-=-"));


An alternative without those scary modulo and ternary operators, just 
because I think it's cute:


import std.range: repeat, roundRobin, take;
import std.algorithm: equal;
assert(roundRobin(repeat('-'), repeat('=')).take(7).equal("-=-=-=-"));


Re: How to use `format` to repeat a character

2016-07-11 Thread Ali Çehreli via Digitalmars-d-learn

On 07/11/2016 02:02 AM, Bahman Movaqar wrote:
> I'm sure I'm missing something very simple but how can I create a string
> like "----" using `format`?

You can't.

> I check the docs on `format` and tried many variations including
> `format("%.*c\n", 4, '-')` but got nowhere.

What makes you expect that format should have that feature? :) Perhaps 
you're familiar with another language's standard library that does that?


> I'd appreciate any hint/help on this.

There are several ways of repeating characters and range elements in 
general:


void main() {
// 'replicate' copies an array (which strings are) eagerly
import std.array : replicate;
assert("-".replicate(3) == "---");

// 'repeat' repeats lazily
import std.range : repeat;
import std.algorithm : equal;
assert('-'.repeat(3).equal("---"));

// Another one that combines multiple range algorithms
import std.range : iota;
import std.algorithm : map;
assert(7.iota.map!(i => i % 2 ? '=' : '-').equal("-=-=-=-"));

// etc.
}

Ali



How to use `format` to repeat a character

2016-07-11 Thread Bahman Movaqar via Digitalmars-d-learn
I'm sure I'm missing something very simple but how can I create a string
like "" using `format`?
I check the docs on `format` and tried many variations including
`format("%.*c\n", 4, '-')` but got nowhere.

I'd appreciate any hint/help on this.

-- 
Bahman Movaqar

http://BahmanM.com - https://twitter.com/bahman__m
https://github.com/bahmanm - https://gist.github.com/bahmanm
PGP Key ID: 0x6AB5BD68 (keyserver2.pgp.com)


Re: Best way to convert Apachelog Datetime 01/Jan/2016:02:25:10 -> 2016-01-01 02:25:10 MySQL-Datetime Format

2016-06-08 Thread Martin Tschierschke via Digitalmars-d-learn
On Wednesday, 8 June 2016 at 10:42:19 UTC, Martin Tschierschke 
wrote:

I know there are easy ways to handle this,
anyone with a code snippet for me?

I found this solution, letting the MySQL engine do the work:

  SELECT STR_TO_DATE('26/Apr/2011:13:21:58', '%d/%b/%Y:%H:%i:%S');

https://www.experts-exchange.com/questions/26992776/convert-apache-log-date-time-to-mysql.html

Maybe  there is an other short solution using the std.datetime 
and or

https://code.dlang.org/packages/dateparser ?


Best way to convert Apachelog Datetime 01/Jan/2016:02:25:10 -> 2016-01-01 02:25:10 MySQL-Datetime Format

2016-06-08 Thread Martin Tschierschke via Digitalmars-d-learn

I know there are easy ways to handle this,
anyone with a code snippet for me?

I would use two regex first to make 01,02,03... from Jan,Feb,.. 
and

second to split the result.

best regards mt.


Re: Unterminated format specifier exception keeps occuring and I don't know why.

2016-01-31 Thread Enjoys Math via Digitalmars-d-learn

On Sunday, 31 January 2016 at 19:51:34 UTC, Enjoys Math wrote:

On Sunday, 31 January 2016 at 19:40:15 UTC, Enjoys Math wrote:
This weird exception keeps occuring and visual D is not 
bringing me to the place in my code that might be calling it.


[...]


The exception is not listed in the Exception Settings checkable 
list.   I will try commenting out the D source code that throws 
it.


The easiest solution is just to ignore the exception and 
continue.  The exception doesn't occur in release mode.


Unterminated format specifier exception keeps occuring and I don't know why.

2016-01-31 Thread Enjoys Math via Digitalmars-d-learn
This weird exception keeps occuring and visual D is not bringing 
me to the place in my code that might be calling it.



Message:

First-chance exception: std.format.FormatException Unterminated 
format specifier: "%" at 
C:\D\dmd2\windows\bin\..\..\src\phobos\std\format.d(830)



I've gotten rid of it before, but I'm not sure how.

Here's the call stack:
KernelBase.dll!76e1d8a8 
 	___LIGHTSHOWAPP.exe!_D2rt9deh_win329throwImplFC6ObjectZv() + 
0x2a bytes	D


___LIGHTSHOWAPP.exe!std.exception.enforceEx!(std.format.FormatException).enforceEx!bool.enforceEx(
 uint line ) Line 618 D
 
	___LIGHTSHOWAPP.exe!std.format.FormatSpec!char.FormatSpec.writeUpToNextSpec!(std.array.Appender!(char[])).writeUpToNextSpec( std.format.FormatSpec!char.FormatSpec* this ) Line 831	D
 
	___LIGHTSHOWAPP.exe!std.format.FormatSpec!char.FormatSpec.__unittestL848_2148.__dgliteral1( void* this ) Line 878 + 0x20 bytes	D
 
	___LIGHTSHOWAPP.exe!std.exception.assertThrown!(std.format.FormatException, bool).assertThrown( uint line ) Line 231 + 0xb bytes	D
 
	___LIGHTSHOWAPP.exe!std.format.FormatSpec!char.FormatSpec.__unittestL848_2148() Line 879	D
 	___LIGHTSHOWAPP.exe!_D15bounded_2d_geom9__modtestFZv() + 0x8 
bytes	D
 
	___LIGHTSHOWAPP.exe!_D4core7runtime18runModuleUnitTestsUZ14__foreachbody1MFPS6object10ModuleInfoZi() + 0x45 bytes	D
 
	___LIGHTSHOWAPP.exe!_D6object10ModuleInfo7opApplyFMDFPS6object10ModuleInfoZiZ9__lambda2MFyPS6object10ModuleInfoZi() + 0xf bytes	D




Re: Unterminated format specifier exception keeps occuring and I don't know why.

2016-01-31 Thread Enjoys Math via Digitalmars-d-learn

On Sunday, 31 January 2016 at 19:40:15 UTC, Enjoys Math wrote:
This weird exception keeps occuring and visual D is not 
bringing me to the place in my code that might be calling it.


[...]


The exception is not listed in the Exception Settings checkable 
list.   I will try commenting out the D source code that throws 
it.


Re: core.time Duration how to get units in double/float format?

2016-01-19 Thread Jonathan M Davis via Digitalmars-d-learn
On Tuesday, January 19, 2016 14:07:50 Borislav Kosharov via Digitalmars-d-learn 
wrote:
> On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis
> wrote:
> > In general, using floating point values with time is an
> > incredibly bad idea. It can certainly make sense when printing
> > stuff out, but using it in calculations is just asking for
> > trouble given all of the unnecessary imprecision that it adds.
> > So, Duration does not directly support floating point values at
> > all, and that's very much on purpose. I'd strongly argue that
> > the fact that TickDuration does was a mistake.
> >
> > So, if you're doing floating point calculations with time, I'd
> > strongly urge you to rethink your code. And if you're just
> > trying to print out the a duration as a floating point value,
> > because it's nice to view that way, then that's fine, but
> > you'll need to do the conversion yourself. And it's not that
> > hard. It just isn't handed to you directly, because aside from
> > printing, code really shouldn't be using floating point values
> > for time. e.g.
> >
> > string toFloatingSeconds(Duration d)
> > {
> > import std.conv;
> > enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
> > auto s = d.split!("seconds", "hnsecs")();
> > return to!string(s.seconds + cast(real)(s.hnsecs) /
> > hnsecsPerSecond);
> > }
> >
> > And yes, that's a bit more of a pain than using to!("seconds",
> > float) with TickDuration, but it's also the sort of thing that
> > most code really shouldn't be doing. So, adding that
> > functionality to Duration would just encourage folks to write
> > buggy code.
> >
> > - Jonathan M Davis
>
> I want to use float time in a game where I call the update method
> passing the delta time as float seconds. It's more easy to
> multiply the dt with a speed constant meaning how much the object
> will move after 1 second. Such float delta time is used in many
> game engines and it's somewhat standart way of doing it. Also the
> method you wrote returns a string and I need a float to multiply
> it with a number.
> Thanks for the reply anyway

Using floating point as a delta for time is inherently buggy. Depending on
what you're doing, you may get away with it, but it means that error is
going to creep into your timing. And the bugs introduced by using floating
point like that can be quite subtle. I would strongly urge anyone using
floating point values with timing like that to rework their code to use
integral values. Aside from TickDuration, all of core.time and std.datetime
quite carefully avoids floating point values - even functions like
convClockFreq (which converts one clock frequency to another).

So, if you want to use floating point values with time stuff, that's
obviously your prerogative, but Duration does not directly support it and
isn't ever going to directly support it precisely because it's error-prone.

- Jonathan M Davis



Re: core.time Duration how to get units in double/float format?

2016-01-19 Thread Borislav Kosharov via Digitalmars-d-learn

On Tuesday, 19 January 2016 at 15:25:58 UTC, wobbles wrote:
On Tuesday, 19 January 2016 at 14:07:50 UTC, Borislav Kosharov 
wrote:
On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis 
wrote:

[...]


I want to use float time in a game where I call the update 
method passing the delta time as float seconds. It's more easy 
to multiply the dt with a speed constant meaning how much the 
object will move after 1 second. Such float delta time is used 
in many game engines and it's somewhat standart way of doing 
it. Also the method you wrote returns a string and I need a 
float to multiply it with a number.

Thanks for the reply anyway


Checkout out how DSFML handles this.
You simply pass around a Clock object that you can restart 
every frame using


clock.restart();

You then call your update function with 
update(Clock.getElapsedTime());
Then in each objects update(Time t) method you just get the 
time in whatever unit you want. Works pretty well.


http://www.dsfml.com/


Yes that's exactly what I'm using. But they changed it to return 
a Duration instead of Time and that broke my code. See the source 
https://github.com/Jebbs/DSFML/blob/master/src/dsfml/system/clock.d#L65


Re: core.time Duration how to get units in double/float format?

2016-01-19 Thread Borislav Kosharov via Digitalmars-d-learn
On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis 
wrote:
In general, using floating point values with time is an 
incredibly bad idea. It can certainly make sense when printing 
stuff out, but using it in calculations is just asking for 
trouble given all of the unnecessary imprecision that it adds. 
So, Duration does not directly support floating point values at 
all, and that's very much on purpose. I'd strongly argue that 
the fact that TickDuration does was a mistake.


So, if you're doing floating point calculations with time, I'd 
strongly urge you to rethink your code. And if you're just 
trying to print out the a duration as a floating point value, 
because it's nice to view that way, then that's fine, but 
you'll need to do the conversion yourself. And it's not that 
hard. It just isn't handed to you directly, because aside from 
printing, code really shouldn't be using floating point values 
for time. e.g.


string toFloatingSeconds(Duration d)
{
import std.conv;
enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
auto s = d.split!("seconds", "hnsecs")();
return to!string(s.seconds + cast(real)(s.hnsecs) / 
hnsecsPerSecond);

}

And yes, that's a bit more of a pain than using to!("seconds", 
float) with TickDuration, but it's also the sort of thing that 
most code really shouldn't be doing. So, adding that 
functionality to Duration would just encourage folks to write 
buggy code.


- Jonathan M Davis


I want to use float time in a game where I call the update method 
passing the delta time as float seconds. It's more easy to 
multiply the dt with a speed constant meaning how much the object 
will move after 1 second. Such float delta time is used in many 
game engines and it's somewhat standart way of doing it. Also the 
method you wrote returns a string and I need a float to multiply 
it with a number.

Thanks for the reply anyway


Re: core.time Duration how to get units in double/float format?

2016-01-19 Thread wobbles via Digitalmars-d-learn
On Tuesday, 19 January 2016 at 14:07:50 UTC, Borislav Kosharov 
wrote:
On Monday, 18 January 2016 at 12:46:31 UTC, Jonathan M Davis 
wrote:

[...]


I want to use float time in a game where I call the update 
method passing the delta time as float seconds. It's more easy 
to multiply the dt with a speed constant meaning how much the 
object will move after 1 second. Such float delta time is used 
in many game engines and it's somewhat standart way of doing 
it. Also the method you wrote returns a string and I need a 
float to multiply it with a number.

Thanks for the reply anyway


Checkout out how DSFML handles this.
You simply pass around a Clock object that you can restart every 
frame using


clock.restart();

You then call your update function with 
update(Clock.getElapsedTime());
Then in each objects update(Time t) method you just get the time 
in whatever unit you want. Works pretty well.


http://www.dsfml.com/


Re: core.time Duration how to get units in double/float format?

2016-01-18 Thread Jonathan M Davis via Digitalmars-d-learn
On Sunday, January 17, 2016 14:43:26 Borislav Kosharov via Digitalmars-d-learn 
wrote:
> Seeing that TickDuration is being deprecated and that I should
> use Duration instead, I faced a problem. I need to get total
> seconds like a float. Using .total!"seconds" returns a long and
> if the duration is less than 1 second I get 0. My question is
> whats the right way to do it. Because I saw that TickDuration has
> a to!("seconds", float) method, but Duration doesn't have one. I
> can convert Duration to TickDuration and call to but seeing that
> its deprecated makes me think there is a better way.

In general, using floating point values with time is an incredibly bad idea.
It can certainly make sense when printing stuff out, but using it in
calculations is just asking for trouble given all of the unnecessary
imprecision that it adds. So, Duration does not directly support floating
point values at all, and that's very much on purpose. I'd strongly argue
that the fact that TickDuration does was a mistake.

So, if you're doing floating point calculations with time, I'd strongly urge
you to rethink your code. And if you're just trying to print out the a
duration as a floating point value, because it's nice to view that way, then
that's fine, but you'll need to do the conversion yourself. And it's not
that hard. It just isn't handed to you directly, because aside from
printing, code really shouldn't be using floating point values for time.
e.g.

string toFloatingSeconds(Duration d)
{
import std.conv;
enum hnsecsPerSecond = convert!("seconds", "hnsecs")(1);
auto s = d.split!("seconds", "hnsecs")();
return to!string(s.seconds + cast(real)(s.hnsecs) / hnsecsPerSecond);
}

And yes, that's a bit more of a pain than using to!("seconds", float) with
TickDuration, but it's also the sort of thing that most code really
shouldn't be doing. So, adding that functionality to Duration would just
encourage folks to write buggy code.

- Jonathan M Davis



Re: core.time Duration how to get units in double/float format?

2016-01-17 Thread Borislav Kosharov via Digitalmars-d-learn

On Sunday, 17 January 2016 at 18:57:13 UTC, biozic wrote:
On Sunday, 17 January 2016 at 14:43:26 UTC, Borislav Kosharov 
wrote:
Seeing that TickDuration is being deprecated and that I should 
use Duration instead, I faced a problem. I need to get total 
seconds like a float. Using .total!"seconds" returns a long 
and if the duration is less than 1 second I get 0. My question 
is whats the right way to do it. Because I saw that 
TickDuration has a to!("seconds", float) method, but Duration 
doesn't have one. I can convert Duration to TickDuration and 
call to but seeing that its deprecated makes me think there is 
a better way.


Why not just use a smaller granularity for Duration.total and 
convert the result?


duration.total!"nsecs" / cast(float) 1e9


Yea I could do that, but its not intuitive to get a total of one 
magnitude to just convert it to another. My question is why 
doesn't `to!` accept Duration.




Re: core.time Duration how to get units in double/float format?

2016-01-17 Thread biozic via Digitalmars-d-learn
On Sunday, 17 January 2016 at 14:43:26 UTC, Borislav Kosharov 
wrote:
Seeing that TickDuration is being deprecated and that I should 
use Duration instead, I faced a problem. I need to get total 
seconds like a float. Using .total!"seconds" returns a long and 
if the duration is less than 1 second I get 0. My question is 
whats the right way to do it. Because I saw that TickDuration 
has a to!("seconds", float) method, but Duration doesn't have 
one. I can convert Duration to TickDuration and call to but 
seeing that its deprecated makes me think there is a better way.


Why not just use a smaller granularity for Duration.total and 
convert the result?


duration.total!"nsecs" / cast(float) 1e9




core.time Duration how to get units in double/float format?

2016-01-17 Thread Borislav Kosharov via Digitalmars-d-learn
Seeing that TickDuration is being deprecated and that I should 
use Duration instead, I faced a problem. I need to get total 
seconds like a float. Using .total!"seconds" returns a long and 
if the duration is less than 1 second I get 0. My question is 
whats the right way to do it. Because I saw that TickDuration has 
a to!("seconds", float) method, but Duration doesn't have one. I 
can convert Duration to TickDuration and call to but seeing that 
its deprecated makes me think there is a better way.


DUB config format: SDLang or JSON?

2015-12-18 Thread Jakob Jenkov via Digitalmars-d-learn
I am just looking at DUB and I can read that there are two config 
formats: SDLang and JSON. Which one is the "new" format? Which 
one is the "future" of DUB?


Re: DUB config format: SDLang or JSON?

2015-12-18 Thread Brad Anderson via Digitalmars-d-learn

On Friday, 18 December 2015 at 22:30:00 UTC, Jakob Jenkov wrote:
I am just looking at DUB and I can read that there are two 
config formats: SDLang and JSON. Which one is the "new" format? 
Which one is the "future" of DUB?


SDLang is the new one. JSON will remain supported. Use whichever 
you like better.


Re: regex format string problem

2015-11-23 Thread Rikki Cattermole via Digitalmars-d-learn

On 23/11/15 9:22 PM, yawniek wrote:

Hi Rikki,

On Monday, 23 November 2015 at 03:57:06 UTC, Rikki Cattermole wrote:

I take it that browscap[0] does it not do what you want?
I have an generator at [1].
Feel free to steal.


This looks interesting, thanks for the hint. However it might be a bit
limited,
i have 15M+ different User Agents with all kind of weird cases,
sometimes not even the extensive ua-core regexs work. (if you're
interested for testing let me know)


Also once you do get yours working, you'll want to use ctRegex and
generate a file with all of them in it. That'll increase performance
significantly.


that was my plan.


Reguarding regex, if you want a named sub part use:
(?[a-z]*)
Where [a-z]* is just an example.

I would recommend you learning how input ranges work. They are used
with how to get the matches out, e.g.

auto rgx = ctRegex!`([a-z])[123]`;
foreach(match; rgx.matchAll("b3")) {
writeln(match.hit);
}


i'm aware how this works, the problem is a different  one:

i do have a second string that contains $n's which can occur in any order.
now of course i can just go and write another regex and replace it, job
done.
but from looking at std.regex this seems to be built in, i just failed
to get it to work properly, see my gist. i hoped this to be a 1liner.


So like this?

import std.regex;
import std.stdio : readln, writeln, write, stdout;

auto REG = ctRegex!(`(\S+)(?: (.*))?`);

void main() {
for(;;) {
write("> ");
stdout.flush;
string line = readln();
line.length--;

if (line.length == 0)
return;

writeln("< ", line.replaceAll(REG, "Unknown program: $1"));
}
}



Re: regex format string problem

2015-11-23 Thread yawniek via Digitalmars-d-learn

Hi Rikki,

On Monday, 23 November 2015 at 03:57:06 UTC, Rikki Cattermole 
wrote:

I take it that browscap[0] does it not do what you want?
I have an generator at [1].
Feel free to steal.


This looks interesting, thanks for the hint. However it might be 
a bit limited,
i have 15M+ different User Agents with all kind of weird cases, 
sometimes not even the extensive ua-core regexs work. (if you're 
interested for testing let me know)


Also once you do get yours working, you'll want to use ctRegex 
and generate a file with all of them in it. That'll increase 
performance significantly.


that was my plan.


Reguarding regex, if you want a named sub part use:
(?[a-z]*)
Where [a-z]* is just an example.

I would recommend you learning how input ranges work. They are 
used with how to get the matches out, e.g.


auto rgx = ctRegex!`([a-z])[123]`;
foreach(match; rgx.matchAll("b3")) {
writeln(match.hit);
}


i'm aware how this works, the problem is a different  one:

i do have a second string that contains $n's which can occur in 
any order.
now of course i can just go and write another regex and replace 
it, job done.
but from looking at std.regex this seems to be built in, i just 
failed to get it to work properly, see my gist. i hoped this to 
be a 1liner.





regex format string problem

2015-11-22 Thread yawniek via Digitalmars-d-learn

hi!

how can i format  a string with captures from a regular 
expression?

basically make this pass:
https://gist.github.com/f17647fb2f8ff2261d42


context: i'm trying to write a implementation for 
https://github.com/ua-parser
where the regular expression as well as the format strings are 
given.





Re: regex format string problem

2015-11-22 Thread Rikki Cattermole via Digitalmars-d-learn

On 23/11/15 12:41 PM, yawniek wrote:

hi!

how can i format  a string with captures from a regular expression?
basically make this pass:
https://gist.github.com/f17647fb2f8ff2261d42


context: i'm trying to write a implementation for
https://github.com/ua-parser
where the regular expression as well as the format strings are given.


I take it that browscap[0] does it not do what you want?
I have an generator at [1].
Feel free to steal.

Also once you do get yours working, you'll want to use ctRegex and 
generate a file with all of them in it. That'll increase performance 
significantly.


Reguarding regex, if you want a named sub part use:
(?[a-z]*)
Where [a-z]* is just an example.

I would recommend you learning how input ranges work. They are used with 
how to get the matches out, e.g.


auto rgx = ctRegex!`([a-z])[123]`;
foreach(match; rgx.matchAll("b3")) {
writeln(match.hit);
}

Or something along those lines, I did it off the top of my head.

[0] 
https://github.com/rikkimax/Cmsed/blob/master/tools/browser_detection/browscap.ini
[1] 
https://github.com/rikkimax/Cmsed/blob/master/tools/browser_detection/generator.d




writef format specifier error message

2015-11-16 Thread ric maicle via Digitalmars-d-learn

I accidentally typed an extra asterisk in the format specifier.
I know that it is wrong but the error isn't clear about what
and where the error is.

import std.stdio;

void main()
{
writef("%*10s", 100);
}

and I got the following error message(s):

$ dmd -run writef.d
std.format.FormatException@std/format.d(978): $ expected

??:? pure @safe bool
std.exception.enforceEx!(std.format.FormatException).enforceEx!(bool).enforceEx(bool,
lazy immutable(char)[], immutable(char)[], ulong) [0x437786]
??:? pure @safe void std.format.FormatSpec!(char).FormatSpec.fillUp()
[0x44754b]
??:? @safe bool
std.format.FormatSpec!(char).FormatSpec.writeUpToNextSpec!(std.stdio.File.LockingTextWriter).writeUpToNextSpec(std.stdio.File.LockingTextWriter)
[0x438173]
??:? @safe uint
std.format.formattedWrite!(std.stdio.File.LockingTextWriter, char,
int).formattedWrite(std.stdio.File.LockingTextWriter, const(char[]),
int) [0x436f99]
??:? @safe void std.stdio.File.writefln!(char,
int).writefln(const(char[]), int) [0x436e9c]
??:? @safe void std.stdio.writefln!(immutable(char)[],
int).writefln(immutable(char)[], int) [0x436deb]
??:? _Dmain [0x436d9f]
??:? _D2rt6dmain211_d_run_mainUiPPaPUAAaZiZ6runAllMFZ9__lambda1MFZv
[0x443cc2]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x443c18]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).runAll() [0x443c7e]
??:? void rt.dmain2._d_run_main(int, char**, extern (C) int
function(char[][])*).tryExec(scope void delegate()) [0x443c18]
??:? _d_run_main [0x443b75]
??:? main [0x43f8d5]
??:? __libc_start_main [0xba3cc60f]


Re: writef format specifier error message

2015-11-16 Thread Ali Çehreli via Digitalmars-d-learn

On 11/16/2015 10:56 AM, ric maicle wrote:

I accidentally typed an extra asterisk in the format specifier.
I know that it is wrong but the error isn't clear about what
and where the error is.

import std.stdio;

void main()
{
writef("%*10s", 100);
}

and I got the following error message(s):

$ dmd -run writef.d
std.format.FormatException@std/format.d(978): $ expected


That message can be improved. Please open a bug report:

  https://issues.dlang.org/enter_bug.cgi

Thank you,
Ali



Re: writef format specifier error message

2015-11-16 Thread ric maicle via Digitalmars-d-learn

On Tuesday, 17 November, 2015 03:49 AM, Ali Çehreli wrote:

On 11/16/2015 10:56 AM, ric maicle wrote:

I accidentally typed an extra asterisk in the format specifier.
I know that it is wrong but the error isn't clear about what
and where the error is.

import std.stdio;

void main()
{
writef("%*10s", 100);
}

and I got the following error message(s):

$ dmd -run writef.d
std.format.FormatException@std/format.d(978): $ expected


That message can be improved. Please open a bug report:

   https://issues.dlang.org/enter_bug.cgi

Thank you,
Ali


Filed: https://issues.dlang.org/show_bug.cgi?id=15348


Re: Bloat with std.(string.)format?

2015-09-18 Thread Kagamin via Digitalmars-d-learn

On Thursday, 17 September 2015 at 15:45:10 UTC, Chris wrote:
I suppose it's an area most people (including myself) shy away 
from. I know next to nothing about compiler implementation.


Sometimes it's just diagnosis of test failures.


Bloat with std.(string.)format?

2015-09-17 Thread Chris via Digitalmars-d-learn

If I have code like this:

auto builder = appender!string;
builder ~= "Hello, World!";
builder ~= "I'm here!";
builder ~= "Now I'm there!";

the object file grows by 10-11 lines with each call to `builder 
~=`. If I use this:


builder ~= format("%s", "Hello, World!");
builder ~= format("%s", "I'm here!");
builder ~= format("%s", "Now I'm there!");

The object file is more than twice as big and it grows by 20 
lines with each call to `format`.


If I use

builder ~= format("%s %s %s", "Hello, World!", "I'm here!", "Now 
I'm there!");


the code bloat is even worse.

There are many situation where a formatting string is preferable 
to concatenation, however it adds _a lot_ of bloat. Would a 
custom formatter be preferable to reduce code bloat or should 
std/format.d be optimized? (Or both?)


dmd 2.067.1
-release -boundscheck=off -inline -O


Re: Bloat with std.(string.)format?

2015-09-17 Thread John Colvin via Digitalmars-d-learn

On Thursday, 17 September 2015 at 09:54:07 UTC, Chris wrote:

If I have code like this:

auto builder = appender!string;
builder ~= "Hello, World!";
builder ~= "I'm here!";
builder ~= "Now I'm there!";

the object file grows by 10-11 lines with each call to `builder 
~=`. If I use this:


builder ~= format("%s", "Hello, World!");
builder ~= format("%s", "I'm here!");
builder ~= format("%s", "Now I'm there!");

The object file is more than twice as big and it grows by 20 
lines with each call to `format`.


If I use

builder ~= format("%s %s %s", "Hello, World!", "I'm here!", 
"Now I'm there!");


the code bloat is even worse.

There are many situation where a formatting string is 
preferable to concatenation, however it adds _a lot_ of bloat. 
Would a custom formatter be preferable to reduce code bloat or 
should std/format.d be optimized? (Or both?)


dmd 2.067.1
-release -boundscheck=off -inline -O


Some initial bloat is expected, format is pretty big (although 
twice as big is a lot, unless your original code was quite 
small?). The extra bloat per call is likely due to inlining. I 
would hope that dmd would spot consecutive inlining of the same 
function and merge them, but perhaps it doesn't.


You could certainly make a less feature complete implementation 
of format that is smaller.


Have you tried with ldc or gdc. In particular, have you tried 
using ldc with --gc-sections on linux?


Re: Bloat with std.(string.)format?

2015-09-17 Thread Chris via Digitalmars-d-learn

On Thursday, 17 September 2015 at 10:33:44 UTC, John Colvin wrote:


Some initial bloat is expected, format is pretty big (although 
twice as big is a lot, unless your original code was quite 
small?).


It was in a test program. Only a few lines. But it would still 
add a lot of bloat in a program that uses it in different 
modules, wouldn't it?


The extra bloat per call is likely due to inlining. I would 
hope that dmd would spot consecutive inlining of the same 
function and merge them, but perhaps it doesn't.


You could certainly make a less feature complete implementation 
of format that is smaller.


Don't know if it's worth the trouble.

Have you tried with ldc or gdc. In particular, have you tried 
using ldc with --gc-sections on linux?


Not yet. GDC and LDC always lag behind (this time considerably), 
so I'm usually stuck with DMD for development.


Re: Bloat with std.(string.)format?

2015-09-17 Thread John Colvin via Digitalmars-d-learn

On Thursday, 17 September 2015 at 10:53:17 UTC, Chris wrote:
On Thursday, 17 September 2015 at 10:33:44 UTC, John Colvin 
wrote:


Some initial bloat is expected, format is pretty big (although 
twice as big is a lot, unless your original code was quite 
small?).


It was in a test program. Only a few lines. But it would still 
add a lot of bloat in a program that uses it in different 
modules, wouldn't it?


The upfront cost is paid only once per unique template arguments 
per binary. So no, it doesn't scale badly there.


Inlining, on the other hand, will - roughly speaking - increase 
binary sizes linearly with the number of calls. That's the cost 
you pay for (hopefully) better performance.


The extra bloat per call is likely due to inlining. I would 
hope that dmd would spot consecutive inlining of the same 
function and merge them, but perhaps it doesn't.


You could certainly make a less feature complete 
implementation of format that is smaller.


Don't know if it's worth the trouble.


I would say not worth it, unless you have a real problem with 
binary sizes for an actual finished product. Even then, I'd say 
you could get bigger, easier gains by messing around with 
-fvisibility settings, --gc-sections, strip etc. on GDC and LDC


Have you tried with ldc or gdc. In particular, have you tried 
using ldc with --gc-sections on linux?


Not yet. GDC and LDC always lag behind (this time 
considerably), so I'm usually stuck with DMD for development.


That's a shame.  
https://github.com/ldc-developers/ldc/releases/tag/v0.16.0-alpha3 
is at 2.067.1, is that not up-to-date enough?


Re: Bloat with std.(string.)format?

2015-09-17 Thread Chris via Digitalmars-d-learn

On Thursday, 17 September 2015 at 15:17:21 UTC, John Colvin wrote:

On Thursday, 17 September 2015 at 13:42:15 UTC, Chris wrote:
On Thursday, 17 September 2015 at 12:49:03 UTC, John Colvin 
wrote:

[...]


Thanks.

That's up to date enough now. Is it stable, though?


Reasonably so in my testing, but expect more bugs than in a 
full release.


For version 2.067.1 it took a long time this time. Maybe we 
should focus some of our efforts on LDC and GCD being up to 
date faster.


It would be great to have more people working on them, yes.


I suppose it's an area most people (including myself) shy away 
from. I know next to nothing about compiler implementation.


Re: Bloat with std.(string.)format?

2015-09-17 Thread John Colvin via Digitalmars-d-learn

On Thursday, 17 September 2015 at 13:42:15 UTC, Chris wrote:
On Thursday, 17 September 2015 at 12:49:03 UTC, John Colvin 
wrote:

[...]


Thanks.

That's up to date enough now. Is it stable, though?


Reasonably so in my testing, but expect more bugs than in a full 
release.


For version 2.067.1 it took a long time this time. Maybe we 
should focus some of our efforts on LDC and GCD being up to 
date faster.


It would be great to have more people working on them, yes.


Re: Bloat with std.(string.)format?

2015-09-17 Thread Chris via Digitalmars-d-learn

On Thursday, 17 September 2015 at 12:49:03 UTC, John Colvin wrote:

On Thursday, 17 September 2015 at 10:53:17 UTC, Chris wrote:
On Thursday, 17 September 2015 at 10:33:44 UTC, John Colvin 
wrote:


Some initial bloat is expected, format is pretty big 
(although twice as big is a lot, unless your original code 
was quite small?).


It was in a test program. Only a few lines. But it would still 
add a lot of bloat in a program that uses it in different 
modules, wouldn't it?


The upfront cost is paid only once per unique template 
arguments per binary. So no, it doesn't scale badly there.


Inlining, on the other hand, will - roughly speaking - increase 
binary sizes linearly with the number of calls. That's the cost 
you pay for (hopefully) better performance.


The extra bloat per call is likely due to inlining. I would 
hope that dmd would spot consecutive inlining of the same 
function and merge them, but perhaps it doesn't.


You could certainly make a less feature complete 
implementation of format that is smaller.


Don't know if it's worth the trouble.


I would say not worth it, unless you have a real problem with 
binary sizes for an actual finished product. Even then, I'd say 
you could get bigger, easier gains by messing around with 
-fvisibility settings, --gc-sections, strip etc. on GDC and LDC


Have you tried with ldc or gdc. In particular, have you tried 
using ldc with --gc-sections on linux?


Not yet. GDC and LDC always lag behind (this time 
considerably), so I'm usually stuck with DMD for development.


That's a shame.  
https://github.com/ldc-developers/ldc/releases/tag/v0.16.0-alpha3 is at 2.067.1, is that not up-to-date enough?


Thanks.

That's up to date enough now. Is it stable, though? For version 
2.067.1 it took a long time this time. Maybe we should focus some 
of our efforts on LDC and GCD being up to date faster.


Re: Speeding up text file parser (BLAST tabular format)

2015-09-15 Thread H. S. Teoh via Digitalmars-d-learn
On Tue, Sep 15, 2015 at 08:55:43AM +, Fredrik Boulund via 
Digitalmars-d-learn wrote:
> On Monday, 14 September 2015 at 18:31:38 UTC, H. S. Teoh wrote:
> >I tried implementing a crude version of this (see code below), and
> >found that manually calling GC.collect() even as frequently as once
> >every 5000 loop iterations (for a 500,000 line test input file) still
> >gives about 15% performance improvement over completely disabling the
> >GC.  Since most of the arrays involved here are pretty small, the
> >frequency could be reduced to once every 50,000 iterations and you'd
> >pretty much get the 20% performance boost for free, and still not run
> >out of memory too quickly.
> 
> Interesting, I'll have to go through your code to understand exactly
> what's going on. I also noticed some GC-related stuff high up in my
> profiling, but had no idea what could be done about that. Appreciate
> the suggestions!

It's very simple, actually. Basically you just call GC.disable() at the
beginning of the program to disable automatic collection cycles, then at
period intervals in you manually trigger collections by calling
GC.collect().

The way I implemented it in my test code was to use a global counter
that I decrement once every loop iteration. When the counter reaches
zero, GC.collect() is called, and then the counter is reset to its
original value.  This is encapsulated in the gcTick() function, so that
it's easy to tweak the frequency of the manual collections without
modifying several different places in the code each time.


T

-- 
BREAKFAST.COM halted...Cereal Port Not Responding. -- YHL


Re: Speeding up text file parser (BLAST tabular format)

2015-09-15 Thread Andrew Brown via Digitalmars-d-learn
I had some luck building a local copy of llvm in my home 
directory, using a linux version about as old as yours (llvm 3.5 
i used) specifying:


--configure --prefix=/home/andrew/llvm

so make install would install it somewhere I had permissions.

Then I changed the cmake command to:

cmake -L -DLLVM_CONFIG="/home/andrew/llvm/bin/llvm-config" ..

and I got a working install of ldc.

Make yourself a cup of tea while you wait though if you try it, 
llvm was about an hour and a half to compile.


On Tuesday, 15 September 2015 at 13:49:04 UTC, Fredrik Boulund 
wrote:
On Tuesday, 15 September 2015 at 10:01:30 UTC, John Colvin 
wrote:
try this: 
https://dlangscience.github.io/resources/ldc-0.16.0-a2_glibc2.11.3.tar.xz


Nope, :(

$ ldd ldc2
./ldc2: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.20' not 
found (required by ./ldc2)

linux-vdso.so.1 =>  (0x7fff2ffd8000)
libpthread.so.0 => /lib64/libpthread.so.0 
(0x00318a00)

libdl.so.2 => /lib64/libdl.so.2 (0x00318a40)
libncurses.so.5 => /lib64/libncurses.so.5 
(0x00319bc0)

librt.so.1 => /lib64/librt.so.1 (0x00318a80)
libz.so.1 => /lib64/libz.so.1 (0x00318ac0)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 
(0x00318dc0)

libm.so.6 => /lib64/libm.so.6 (0x003189c0)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 
(0x00318c00)

libc.so.6 => /lib64/libc.so.6 (0x00318980)
/lib64/ld-linux-x86-64.so.2 (0x00318940)
libtinfo.so.5 => /lib64/libtinfo.so.5 
(0x00319900)


Thanks for trying though!





Re: Speeding up text file parser (BLAST tabular format)

2015-09-15 Thread Rikki Cattermole via Digitalmars-d-learn

On 15/09/15 9:00 PM, Kagamin wrote:

On Tuesday, 15 September 2015 at 08:51:02 UTC, Fredrik Boulund wrote:

Using char[] all around might be a good idea, but it doesn't seem like
the string conversions are really that taxing. What are the arguments
for working on char[] arrays rather than strings?


No, casting to string would be incorrect here because the line buffer is
reused, your original usage of to!string is correct.


I made the assumption it wasn't allocating. Ehhh.


Re: Speeding up text file parser (BLAST tabular format)

2015-09-15 Thread John Colvin via Digitalmars-d-learn

On Tuesday, 15 September 2015 at 09:09:00 UTC, Kagamin wrote:
On Tuesday, 15 September 2015 at 08:53:37 UTC, Fredrik Boulund 
wrote:

my favourite for streaming a file:
enum chunkSize = 4096;
File(fileName).byChunk(chunkSize).map!"cast(char[])a".joiner()


Is this an efficient way of reading this type of file? What 
should one keep in mind when choosing chunkSize?


reasonably efficient, yes. See http://stackoverflow.com/a/237495 
for a discussion of chunk sizing when streaming a file.


It provides you only one char at a time instead of a whole 
line. It will be quite constraining for your code if not 
mind-bending.


http://dlang.org/phobos/std_string.html#.lineSplitter

File(fileName).byChunk(chunkSize).map!"cast(char[])a".joiner().lineSplitter()


Re: Speeding up text file parser (BLAST tabular format)

2015-09-15 Thread Fredrik Boulund via Digitalmars-d-learn

On Monday, 14 September 2015 at 18:31:38 UTC, H. S. Teoh wrote:
I tried implementing a crude version of this (see code below), 
and found that manually calling GC.collect() even as frequently 
as once every 5000 loop iterations (for a 500,000 line test 
input file) still gives about 15% performance improvement over 
completely disabling the GC.  Since most of the arrays involved 
here are pretty small, the frequency could be reduced to once 
every 50,000 iterations and you'd pretty much get the 20% 
performance boost for free, and still not run out of memory too 
quickly.


Interesting, I'll have to go through your code to understand 
exactly what's going on. I also noticed some GC-related stuff 
high up in my profiling, but had no idea what could be done about 
that. Appreciate the suggestions!




Re: Speeding up text file parser (BLAST tabular format)

2015-09-15 Thread Fredrik Boulund via Digitalmars-d-learn
On Monday, 14 September 2015 at 16:13:14 UTC, Edwin van Leeuwen 
wrote:
See this link for clarification on what the columns/numbers in 
the profile file mean

http://forum.dlang.org/post/f9gjmo$2gce$1...@digitalmars.com

It is still difficult to parse though. I myself often use 
sysprof (only available on linux), which automatically ranks by 
time spent.


Thanks for the link. I read up on what everything means, but I 
think the problem isn't finding what consumes the most time, the 
problem is me not knowing the standard library well enough to 
translate the largest consumers to actual parts of my code :).


<    1   2   3   4   >