Re: Code fails with linker error. Why?

2014-10-06 Thread eles via Digitalmars-d-learn

On Saturday, 4 October 2014 at 15:29:57 UTC, John Colvin wrote:
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 04 Oct 2014 11:01:28 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

On Saturday, 4 October 2014 at 10:38:32 UTC, ketmar via 
Digitalmars-d-learn wrote:

 On Sat, 04 Oct 2014 10:27:16 +
 John Colvin via Digitalmars-d-learn 
 digitalmars-d-learn@puremagic.com

 wrote:



I don't really see the point though.

class A
{
void foo(int a) { Afoo(this, a); }
}

then declare and define Afoo however you like.


That's hackish, bad and convoluted. And it does not/should not 
allow one to mess with the private fields of the class, 
especially if Afoo is defined in another module.


And on that matter, a function that is to be provided by another 
module should be explicitly marked as such in its declaration.


Otherwise, I could declare a function, forget to provide its 
definition, still having the surprise that the code compiles and 
runs with very strange results because some other module provides 
a function that just happens to work.


Let's not even say how messy this could get with version() where 
you could disable a function definition by error, in one version, 
but still have a software that compiles and runs nowhere.


Re: Curl - Set cookie value

2014-10-06 Thread andre via Digitalmars-d-learn
I found out the real issue. Using addRequestHeader also works for 
cookies, but there is an issue with HTTP attribute 
responseHeaders.
As this attribute is an associative array (string[string]) it 
contains only 1 value for 1 key.
In case there are several HTTP keys with the same name, it only 
contains the last value.

Example:

set-cookie = A; path=/; HttpOnly
set-cookie = B; path=/; HttpOnly
content-type = text/plain

reponseHeaders[set-cookie] contains only the seccond value.
To work around this issue you have to react on onReceiveHeader
and catch the values there.

Kind regards
André

On Monday, 6 October 2014 at 06:43:58 UTC, andre wrote:

Hi,

from a GET request I receive an value set-cookie in the 
reponse header. The value from set-cookie I have to provide 
as cookie value for the next post request. CURL provides a 
command line command

  --cookie USER_TOKEN=Yes

In the std.net.curl HTTP API, there is no possibility to set a 
cookie value.


Could you check?

Kind regards
André




Re: curl and proxy

2014-10-06 Thread Sag Academy via Digitalmars-d-learn

On Friday, 3 October 2014 at 10:53:27 UTC, Marc Schütz wrote:

On Friday, 3 October 2014 at 04:57:28 UTC, AntonSotov wrote:

 auto http = HTTP(dlang.org);
 http.onReceive = (ubyte[] data)
 {
   writeln(cast(string) (data));
   return data.length;
 };
 http.proxy = 192.168.111.111;
 http.proxyPort = 1788;

 WHAT HERE ?

 http.perform();
 //
how to make Сurl authorize on a proxy.
I specify proxyUser and proxyPassword?


I think there's currently no way. curl provides this as an 
option (CurlOption.proxyuserpwd):


curl.set(CurlOption.proxyuserpwd, myuser:mypasswd);

But unfortunately the `curl` struct is a private member of 
`HTTP`...



satisfied with your answer.


array append result type

2014-10-06 Thread John Colvin via Digitalmars-d-learn

string a;
char[] b;
pragma(msg, typeof(a ~ b)); // char[]

why not string?

What are the rules that determine this?


Re: array append result type

2014-10-06 Thread monarch_dodra via Digitalmars-d-learn

On Monday, 6 October 2014 at 11:28:16 UTC, John Colvin wrote:

string a;
char[] b;
pragma(msg, typeof(a ~ b)); // char[]

why not string?

What are the rules that determine this?


*Ideally*, I'd have said it returns char[], so that you can 
chose via purity.


However, it's not pure, so that argument doesn't hold.

That said, casting to immutable is safe provided you never 
actually mutate. The reverse isn't true.


To answer the question directly though, I don't know what the 
rules are. I'd guess they are mostly whatever druntime 
implemented though...


Re: Code fails with linker error. Why?

2014-10-06 Thread John Colvin via Digitalmars-d-learn

On Monday, 6 October 2014 at 10:10:04 UTC, eles wrote:

On Saturday, 4 October 2014 at 15:29:57 UTC, John Colvin wrote:
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 04 Oct 2014 11:01:28 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

On Saturday, 4 October 2014 at 10:38:32 UTC, ketmar via 
Digitalmars-d-learn wrote:

 On Sat, 04 Oct 2014 10:27:16 +
 John Colvin via Digitalmars-d-learn 
 digitalmars-d-learn@puremagic.com

 wrote:



I don't really see the point though.

class A
{
   void foo(int a) { Afoo(this, a); }
}

then declare and define Afoo however you like.


That's hackish, bad and convoluted.


I disagree. It's simple and easy to understand.

And it does not/should not allow one to mess with the private 
fields of the class, especially if Afoo is defined in another 
module.


s/especially/only

This is the only genuine problem I can see that requires a 
language extension. Separating class definition from method 
definition in to different compilation units doesn't allow access 
to private members without very nasty hacks.


And on that matter, a function that is to be provided by 
another module should be explicitly marked as such in its 
declaration.


Otherwise, I could declare a function, forget to provide its 
definition, still having the surprise that the code compiles 
and runs with very strange results because some other module 
provides a function that just happens to work.


I don't quite follow. Example?

Let's not even say how messy this could get with version() 
where you could disable a function definition by error, in one 
version, but still have a software that compiles and runs 
nowhere.


Re: How to detect start of Unicode symbol and count amount of graphemes

2014-10-06 Thread Nicolas F. via Digitalmars-d-learn

Unicode is hard to deal with properly as how you deal with it is
very context dependant.

One grapheme is a visible character and consists of one or more
codepoints. One codepoint is one mapping of a byte sequence to a
meaning, and consists of one or more bytes.

This you do not want to deal with yourself, as knowing which
codepoints form graphemes is hard. Thankfully, std.uni exists.
Specifically, look at decodeGrapheme: it pops one grapheme from
an input range and returns it.

Never write code that deals with unicode on a bytelevel. It will
always be wrong.


Template Mixin Conflicts

2014-10-06 Thread Alice via Digitalmars-d-learn

Hi,


I've created a template mixin which contains a struct definition. 
The template is meant to be mixed into each module - to provide a 
little bit of auto generated info in each module - generated at 
compile time. As soon as I reference the symbols in any context, 
it starts complaining about conflicts between the two modules.


It doesn't have to be a struct; the conflicts exist whether I 
define a struct, class, function, or whatever... any symbol.


It is valid to manually define symbols with the same name in 
different modules, since they each exist in a different scope, so 
why does a symbol brought in by a mixin conflict?


Even defining the struct as private doesn't help. Any ideas would 
be most appreciated.



Here's a very simple failing case:


// ModuleA.d
module ModuleA;

import ModuleB;

mixin MixinUsefulStuff;

pragma(msg, a = , SomeData.a); // Error: 
ModuleB.MixinUsefulStuff!().SomeData at ModuleB.d(5) conflicts 
with ModuleA.MixinUsefulStuff!().SomeData at ModuleB.d(5)



// ModuleB.d
module ModuleB;

mixin template MixinUsefulStuff()
{
struct SomeData
{
enum a = 123;
}
}

mixin MixinUsefulStuff;


Re: Code fails with linker error. Why?

2014-10-06 Thread eles via Digitalmars-d-learn

On Monday, 6 October 2014 at 11:54:56 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 10:10:04 UTC, eles wrote:

On Saturday, 4 October 2014 at 15:29:57 UTC, John Colvin wrote:
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 04 Oct 2014 11:01:28 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

On Saturday, 4 October 2014 at 10:38:32 UTC, ketmar via 
Digitalmars-d-learn wrote:

 On Sat, 04 Oct 2014 10:27:16 +
 John Colvin via Digitalmars-d-learn 
 digitalmars-d-learn@puremagic.com

 wrote:



I don't quite follow. Example?


Well, in the OP example, imagine that I was trying to compile 
this module along with another one that simply happened to have a 
method defined in a way that the linker would have find it.


I would have compiled with:

dmd app.d app2.d

and be unaware what bug I have introduced because I forgot do 
declare formula() as abstract in the first class.


Re: Template Mixin Conflicts

2014-10-06 Thread Alice via Digitalmars-d-learn
And just this second I found this... but it's a few months old 
and has no follow up.


http://forum.dlang.org/thread/mailman.1054.1398548687.2763.digitalmars-d-b...@puremagic.com


On Monday, 6 October 2014 at 12:17:01 UTC, Alice wrote:

Hi,


I've created a template mixin which contains a struct 
definition. The template is meant to be mixed into each module 
- to provide a little bit of auto generated info in each module 
- generated at compile time. As soon as I reference the symbols 
in any context, it starts complaining about conflicts between 
the two modules.


It doesn't have to be a struct; the conflicts exist whether I 
define a struct, class, function, or whatever... any symbol.


It is valid to manually define symbols with the same name in 
different modules, since they each exist in a different scope, 
so why does a symbol brought in by a mixin conflict?


Even defining the struct as private doesn't help. Any ideas 
would be most appreciated.



Here's a very simple failing case:


// ModuleA.d
module ModuleA;

import ModuleB;

mixin MixinUsefulStuff;

pragma(msg, a = , SomeData.a); // Error: 
ModuleB.MixinUsefulStuff!().SomeData at ModuleB.d(5) conflicts 
with ModuleA.MixinUsefulStuff!().SomeData at ModuleB.d(5)



// ModuleB.d
module ModuleB;

mixin template MixinUsefulStuff()
{
struct SomeData
{
enum a = 123;
}
}

mixin MixinUsefulStuff;




Re: Code fails with linker error. Why?

2014-10-06 Thread ketmar via Digitalmars-d-learn
On Mon, 06 Oct 2014 11:54:55 +
John Colvin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 I disagree. It's simple and easy to understand.
and hackish.

 This is the only genuine problem I can see that requires a 
 language extension. Separating class definition from method 
 definition in to different compilation units doesn't allow access 
 to private members without very nasty hacks.
no hacks needed.

== pkg.classdef.d ==
  module pkg.classdef;
  class A {
  private:
int hiddenField;
int bar ();
  }


== pkg.othermodule.d ===
  module pkg.othermodule;
  import pkg.classdef;
  // this imlements A.bar() declared in pkg.classdef:
  @implementation(A) int bar () { return this.hiddenField; }

compiler is perfectly able to put A.bar() implementation in the scope
of A, and then A.bar can access anything in A. it's the same as
declaring bar() in class definition.


signature.asc
Description: PGP signature


Re: Code fails with linker error. Why?

2014-10-06 Thread John Colvin via Digitalmars-d-learn

On Monday, 6 October 2014 at 12:16:14 UTC, eles wrote:

On Monday, 6 October 2014 at 11:54:56 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 10:10:04 UTC, eles wrote:
On Saturday, 4 October 2014 at 15:29:57 UTC, John Colvin 
wrote:
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 04 Oct 2014 11:01:28 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:

On Saturday, 4 October 2014 at 10:38:32 UTC, ketmar via 
Digitalmars-d-learn wrote:

 On Sat, 04 Oct 2014 10:27:16 +
 John Colvin via Digitalmars-d-learn 
 digitalmars-d-learn@puremagic.com

 wrote:



I don't quite follow. Example?


Well, in the OP example, imagine that I was trying to compile 
this module along with another one that simply happened to have 
a method defined in a way that the linker would have find it.


I would have compiled with:

dmd app.d app2.d

and be unaware what bug I have introduced because I forgot do 
declare formula() as abstract in the first class.


This isn't a problem. You're not going to get the name-mangling 
right by accident. Even for free functions the module name is 
mangled in.


The only way this can happen is with extern(C) functions, which 
is because C doesn't mangle it's function names.


Re: How to detect start of Unicode symbol and count amount of graphemes

2014-10-06 Thread Kagamin via Digitalmars-d-learn

On Sunday, 5 October 2014 at 12:09:34 UTC, Uranuz wrote:
Maybe there is some idea how to just detect first code unit of 
grapheme without overhead for using Grapheme struct? I just 
tried to check if ch  128 (for UTF-8). But this dont work. How 
to check if byte is continuation of code for single code point 
or if new sequence started?


Are you trying to split strings? If you want to optimize usage of 
graphemes, try to check if 10 code units contain ascii symbol; 
when that fails, fall back to graphemes.


Re: Template Mixin Conflicts

2014-10-06 Thread Alice via Digitalmars-d-learn

btw, do you mind to fill a bugreport?


Okay, will do so once I get home from work. Thanks for the scope 
tip. Will try that out later too.




Re: Code fails with linker error. Why?

2014-10-06 Thread John Colvin via Digitalmars-d-learn
On Monday, 6 October 2014 at 12:36:41 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Mon, 06 Oct 2014 11:54:55 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:


I disagree. It's simple and easy to understand.

and hackish.


D is very amenable to slightly hackish code.

This is the only genuine problem I can see that requires a 
language extension. Separating class definition from method 
definition in to different compilation units doesn't allow 
access to private members without very nasty hacks.



no hacks needed.


I meant that without a language change, one does need hacks.


== pkg.classdef.d ==
  module pkg.classdef;
  class A {
  private:
int hiddenField;
int bar ();
  }


== pkg.othermodule.d ===
  module pkg.othermodule;
  import pkg.classdef;
  // this imlements A.bar() declared in pkg.classdef:
  @implementation(A) int bar () { return this.hiddenField; }

compiler is perfectly able to put A.bar() implementation in the 
scope

of A, and then A.bar can access anything in A. it's the same as
declaring bar() in class definition.


That would be nice, although personally I'd prefer

int A.bar() { return this.hiddenField; }

as it's less verbose and would be familiar to c++ programmers.


Re: Code fails with linker error. Why?

2014-10-06 Thread ketmar via Digitalmars-d-learn
On Mon, 06 Oct 2014 15:44:34 +
John Colvin via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

  I disagree. It's simple and easy to understand.
  and hackish.
 D is very amenable to slightly hackish code.
D allows to write hackish code, and it's good. but if we can provide a
way to write less hackish code, it's good and preferable, i think. ;-)

  no hacks needed.
 I meant that without a language change, one does need hacks.
i see.

 That would be nice, although personally I'd prefer
 
 int A.bar() { return this.hiddenField; }
 
 as it's less verbose and would be familiar to c++ programmers.
i was trying to not change grammar, that's why i invented that
horribly-looking @implementation attribute. this way the code is still
marked as a hack, but officially endorsed hack. besides, attribute
allows to use code blocks:

  @implementation(A) {
int foo () { ... }
void bar () { ... }
  }

or even:

  @implementation(A):
int foo () { ... }
void bar () { ... }

sure, it has it's own pack of problems:

  @implementation(A) {
@implementation(B) {
  // I'M LOST!
}
  }

but we can have both! ;-)


signature.asc
Description: PGP signature


Re: Code fails with linker error. Why?

2014-10-06 Thread eles via Digitalmars-d-learn

On Monday, 6 October 2014 at 15:44:36 UTC, John Colvin wrote:
On Monday, 6 October 2014 at 12:36:41 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Mon, 06 Oct 2014 11:54:55 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:


I disagree. It's simple and easy to understand.

and hackish.


D is very amenable to slightly hackish code.

This is the only genuine problem I can see that requires a 
language extension. Separating class definition from method 
definition in to different compilation units doesn't allow 
access to private members without very nasty hacks.



no hacks needed.


I meant that without a language change, one does need hacks.


So, you admit it is a hack! Gotcha!


Re: Code fails with linker error. Why?

2014-10-06 Thread eles via Digitalmars-d-learn

On Monday, 6 October 2014 at 13:23:55 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 12:16:14 UTC, eles wrote:

On Monday, 6 October 2014 at 11:54:56 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 10:10:04 UTC, eles wrote:
On Saturday, 4 October 2014 at 15:29:57 UTC, John Colvin 
wrote:
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 04 Oct 2014 11:01:28 +
John Colvin via Digitalmars-d-learn


This isn't a problem. You're not going to get the name-mangling 
right by accident. Even for free functions the module name is 
mangled in.


It is. I could erase the definition of an identical class in 
another .d file and accidentally leave an orphan definition 
method therein.


The only way this can happen is with extern(C) functions, which 
is because C doesn't mangle it's function names.


This too is a hole. Why to leave holes?

I like the fromage à trous:

http://fr.wikipedia.org/wiki/Paradoxe_du_fromage_à_trous

But not in my software.



Re: array append result type

2014-10-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/6/14 7:28 AM, John Colvin wrote:

string a;
char[] b;
pragma(msg, typeof(a ~ b)); // char[]

why not string?


It really should be whatever you want. a ~ b is going to generate a 
completely unique independent copy of a and b.



What are the rules that determine this?


Not sure. I would have expected at least one of a ~ b or  b ~ a to be 
string, but both generate char[].


I filed this ER ages ago: https://issues.dlang.org/show_bug.cgi?id=1654

Not sure if anyone has it on their radar at this point.

-Steve


Re: array append result type

2014-10-06 Thread monarch_dodra via Digitalmars-d-learn
On Monday, 6 October 2014 at 16:38:37 UTC, Steven Schveighoffer 
wrote:
I filed this ER ages ago: 
https://issues.dlang.org/show_bug.cgi?id=1654


Not sure if anyone has it on their radar at this point.

-Steve


I didn't read the whole thing, but wouldn't purity be a major 
game changer for 1654?


Re: Code fails with linker error. Why?

2014-10-06 Thread John Colvin via Digitalmars-d-learn

On Monday, 6 October 2014 at 16:02:40 UTC, eles wrote:

On Monday, 6 October 2014 at 13:23:55 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 12:16:14 UTC, eles wrote:

On Monday, 6 October 2014 at 11:54:56 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 10:10:04 UTC, eles wrote:
On Saturday, 4 October 2014 at 15:29:57 UTC, John Colvin 
wrote:
On Saturday, 4 October 2014 at 11:19:52 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Sat, 04 Oct 2014 11:01:28 +
John Colvin via Digitalmars-d-learn


This isn't a problem. You're not going to get the 
name-mangling right by accident. Even for free functions the 
module name is mangled in.


It is. I could erase the definition of an identical class in 
another .d file and accidentally leave an orphan definition 
method therein.


Unless I misunderstand you, this can't happen. Class methods are 
mangled to contain the module name and the class name. I can't 
think of a way of getting the same mangling and getting a bad 
substitution without either


a) pragma(mangle, ...)

or

b) you have 2 modules with the same name, containing classes with 
the same name, which you compile completely separately (to 
prevent the compiler complaining about the duplication), then 
link the results together. This would also work for free 
functions.


a) is a deliberate feature. b) has bypassed the compiler 
completely, there's no way it can know.


You're never going to do it by accident.

The only way this can happen is with extern(C) functions, 
which is because C doesn't mangle it's function names.


This too is a hole. Why to leave holes?


Because it's how C linkage works and if we're using extern(C), it 
means we want C linkage. That's the point of extern(C).


Search Engine

2014-10-06 Thread ANtlord via Digitalmars-d-learn
Good day! I recenlty have tried create typical project on vibe.d. 
The web framework is not bad. And I can say, that it is better 
that something another web frameworks. But I have met a problem. 
I can't find search engine.


I use xapian always. It has API for several languages, but except 
D. I know about another search engines, but I can't find even one 
for D. Does D have search engine? Or Have I find theme for 
starting open source project?


Best Regards. Sorry for my english.


Re: array append result type

2014-10-06 Thread Steven Schveighoffer via Digitalmars-d-learn

On 10/6/14 1:01 PM, monarch_dodra wrote:

On Monday, 6 October 2014 at 16:38:37 UTC, Steven Schveighoffer wrote:

I filed this ER ages ago: https://issues.dlang.org/show_bug.cgi?id=1654

Not sure if anyone has it on their radar at this point.

-Steve


I didn't read the whole thing, but wouldn't purity be a major game
changer for 1654?


Of course! It was immediately what I thought of when purity = unique was 
introduced (that and dup/idup).


This aspect has not been mentioned on the thread, but that issue is 
really old.


-Steve


Re: How to detect start of Unicode symbol and count amount of graphemes

2014-10-06 Thread Uranuz via Digitalmars-d-learn


Have a look here [1]. For example, if you have a byte that is 
between U+0080 and U+07FF you know that you need two bytes to 
get that whole code point.


[1] http://en.wikipedia.org/wiki/UTF-8#Description


Thanks. I solved it myself already for UTF-8 encoding. There 
choosed approach with using bitbask. Maybe it is not best with 
eficiency but it works)


( str[index]  0b1000 ) == 0 ||
( str[index]  0b1110 ) == 0b1100 ||
( str[index]  0b ) == 0b1110 ||
( str[index]  0b1000 ) == 0b

If it is true it means that first byte of sequence found and I 
can count them. Am I right that it equals to number of graphemes, 
or are there some exceptions from this rule?


For UTF-32 number of codeUnits is just equal to number of 
graphemes. And what about UTF-16? Is it possible to detect first 
codeUnit of encoding sequence?


Re: Template Mixin Conflicts

2014-10-06 Thread Ali Çehreli via Digitalmars-d-learn

On 10/06/2014 05:50 AM, ketmar via Digitalmars-d-learn wrote:

On Mon, 06 Oct 2014 12:17:00 +
Alice via Digitalmars-d-learn digitalmars-d-learn@puremagic.com wrote:

the joy of mixin bugs, yeah! it's a bug in compiler: it instantiates
mixin in invalid scope. for now you can use string mixins, they are
working fine.

you also can use this to fix scoping:

=== ModuleA.d ===

   module ModuleA;
   static import ModuleB;
   mixin ModuleB.MixinUsefulStuff;
   pragma(msg, a = , SomeData.a);

btw, do you mind to fill a bugreport?



Another workaround is to mixin into a scope and then bring the names out 
by alias. Doing the following in both files works:


struct MixinUsefulStuffWrapper
{
mixin MixinUsefulStuff;
}

alias SomeData = MixinUsefulStuffWrapper.SomeData;

Ali



Re: How to detect start of Unicode symbol and count amount of graphemes

2014-10-06 Thread ketmar via Digitalmars-d-learn
On Mon, 06 Oct 2014 17:28:43 +
Uranuz via Digitalmars-d-learn digitalmars-d-learn@puremagic.com
wrote:

 If it is true it means that first byte of sequence found and I 
 can count them. Am I right that it equals to number of graphemes, 
 or are there some exceptions from this rule?
alot. take for example RIGHT-TO-LEFT MARK, which is not a grapheme at
all. and not a composite for that matter. ah, those joys of unicode!


signature.asc
Description: PGP signature


Re: How to detect start of Unicode symbol and count amount of graphemes

2014-10-06 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Oct 06, 2014 at 05:28:43PM +, Uranuz via Digitalmars-d-learn wrote:
 
 Have a look here [1]. For example, if you have a byte that is between
 U+0080 and U+07FF you know that you need two bytes to get that whole
 code point.
 
 [1] http://en.wikipedia.org/wiki/UTF-8#Description
 
 Thanks. I solved it myself already for UTF-8 encoding. There choosed
 approach with using bitbask. Maybe it is not best with eficiency but
 it works)
 
 ( str[index]  0b1000 ) == 0 ||
 ( str[index]  0b1110 ) == 0b1100 ||
 ( str[index]  0b ) == 0b1110 ||
 ( str[index]  0b1000 ) == 0b
 
 If it is true it means that first byte of sequence found and I can
 count them. Am I right that it equals to number of graphemes, or are
 there some exceptions from this rule?
 
 For UTF-32 number of codeUnits is just equal to number of graphemes.
 And what about UTF-16? Is it possible to detect first codeUnit of
 encoding sequence?

This looks wrong to me. Are you sure this finds *all* possible
graphemes? Keep in mind that combining diacritic sequences are treated
as a single grapheme; for example the sequence 'A' U+0301 U+0302 U+0303.
There are several different codepoint ranges that have the combining
diacritic property, and they are definitely more complicated than what
you have here.

Furthermore, there are more complicated things like the Devanagari
sequences (e.g., KA + VIRAMA + TA + VOWEL SIGN U), that your code
certainly doesn't look like it would handle correctly.

As somebody else has said, it's generally a bad idea to work with
Unicode byte sequences yourself, because Unicode is complicated, and
many apparently-simple concepts actually require a lot of care to get it
right.


T

-- 
It won't be covered in the book. The source code has to be useful for 
something, after all. -- Larry Wall


Re: array append result type

2014-10-06 Thread Ali Çehreli via Digitalmars-d-learn

On 10/06/2014 04:40 AM, monarch_dodra wrote:

On Monday, 6 October 2014 at 11:28:16 UTC, John Colvin wrote:

string a;
char[] b;
pragma(msg, typeof(a ~ b)); // char[]

why not string?

What are the rules that determine this?


*Ideally*, I'd have said it returns char[], so that you can chose via
purity.

However, it's not pure, so that argument doesn't hold.


Array concatenation should be considered pure because it is the 
equivalent of the following function, which currently works:


pure char[] cat(string a, const(char)[] b)
{
return a ~ b;
}

void main()
{
string a;
char[] b;

char[] c = cat(a, b);// works
string s = cat(a, b);// works
}

  http://dlang.org/function.html#pure-functions

We should bring the above function and the ~ operator in line with each 
other. I haven't followed the discussions on GC usage in purity closely 
but if 'new' is allowed, GC allocations should be allowed as well.


Ali



Re: How to detect start of Unicode symbol and count amount of graphemes

2014-10-06 Thread anonymous via Digitalmars-d-learn

On Monday, 6 October 2014 at 17:28:45 UTC, Uranuz wrote:

( str[index]  0b1000 ) == 0 ||
( str[index]  0b1110 ) == 0b1100 ||
( str[index]  0b ) == 0b1110 ||
( str[index]  0b1000 ) == 0b

If it is true it means that first byte of sequence found and I 
can count them. Am I right that it equals to number of 
graphemes, or are there some exceptions from this rule?


For UTF-32 number of codeUnits is just equal to number of 
graphemes. And what about UTF-16? Is it possible to detect 
first codeUnit of encoding sequence?


I think your idea of graphemes is off.

A grapheme is made up of one or more code points. This is the
same for all UTF encodings.
A code point is made of one or more code units. UTF8: between 1
and 4 I think, UTF16: 1 or 2, UTF32: always 1.
A code unit is made up of a fixed number of bytes. UTF8: 1,
UTF16: 2, UTF32: 4.

So, the number of UTF8 bytes in a sequence has no relation to
graphemes. The number of leading ones in a UTF8 start byte is
equal to the total number of bytes in that sequence. I.e. when
you see a 0b1110_ byte, the following two bytes should be
continuation bytes (0b10xx_), and the three of them together
encode a *code point*.

And in UTF32, the number of code units is equal to the number of
*code points*, not graphemes.


Re: Search Engine

2014-10-06 Thread yawniek via Digitalmars-d-learn

On Monday, 6 October 2014 at 17:11:51 UTC, ANtlord wrote:
Good day! I recenlty have tried create typical project on 
vibe.d. The web framework is not bad. And I can say, that it is 
better that something another web frameworks. But I have met a 
problem. I can't find search engine.


I use xapian always. It has API for several languages, but 
except D. I know about another search engines, but I can't find 
even one for D. Does D have search engine? Or Have I find theme 
for starting open source project?


Best Regards. Sorry for my english.


you should be able to include xapian via c++ extern interface
see: http://dlang.org/cpp_interface.html

or you can use std.json to talk to elasticsearch.


Re: Code fails with linker error. Why?

2014-10-06 Thread John Colvin via Digitalmars-d-learn

On Monday, 6 October 2014 at 16:04:02 UTC, eles wrote:

On Monday, 6 October 2014 at 15:44:36 UTC, John Colvin wrote:
On Monday, 6 October 2014 at 12:36:41 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Mon, 06 Oct 2014 11:54:55 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com

wrote:


I disagree. It's simple and easy to understand.

and hackish.


D is very amenable to slightly hackish code.

This is the only genuine problem I can see that requires a 
language extension. Separating class definition from method 
definition in to different compilation units doesn't allow 
access to private members without very nasty hacks.



no hacks needed.


I meant that without a language change, one does need hacks.


So, you admit it is a hack! Gotcha!


In order to get access to private variables from another 
compilation unit, yes, you need some nasty hackiness. I haven't 
presented such a hack though.


Re: Code fails with linker error. Why?

2014-10-06 Thread eles via Digitalmars-d-learn

On Monday, 6 October 2014 at 19:03:13 UTC, John Colvin wrote:

On Monday, 6 October 2014 at 16:04:02 UTC, eles wrote:

On Monday, 6 October 2014 at 15:44:36 UTC, John Colvin wrote:
On Monday, 6 October 2014 at 12:36:41 UTC, ketmar via 
Digitalmars-d-learn wrote:

On Mon, 06 Oct 2014 11:54:55 +
John Colvin via Digitalmars-d-learn 
digitalmars-d-learn@puremagic.com


In order to get access to private variables from another 
compilation unit, yes, you need some nasty hackiness. I haven't 
presented such a hack though.


I know. Was not serious.



slidingSplitter + retro

2014-10-06 Thread Nordlöw

I've almost satisfied with my new range slidingSplitter at

https://github.com/nordlow/justd/blob/master/range_ex.d#L19

All unittest work as expected except my radial test

https://github.com/nordlow/justd/blob/master/range_ex.d#L243

which, I believe, incorrectly prints

Tuple!(int[], int[])([1, 2], [3])
Tuple!(int[], int[])([1, 2], [3])
Tuple!(int[], int[])([1], [2, 3])
Tuple!(int[], int[])([1, 2, 3], [])
Tuple!(int[], int[])([], [1, 2, 3])

I cannot understand why the line

Tuple!(int[], int[])([1, 2], [3])

is printed twice at the beginning.

Have I done something wrong in my implementation of opIndex or 
opSlice?


Re: slidingSplitter + retro

2014-10-06 Thread monarch_dodra via Digitalmars-d-learn

On Monday, 6 October 2014 at 20:06:41 UTC, Nordlöw wrote:

I've almost satisfied with my new range slidingSplitter at

https://github.com/nordlow/justd/blob/master/range_ex.d#L19

All unittest work as expected except my radial test

https://github.com/nordlow/justd/blob/master/range_ex.d#L243

which, I believe, incorrectly prints

Tuple!(int[], int[])([1, 2], [3])
Tuple!(int[], int[])([1, 2], [3])
Tuple!(int[], int[])([1], [2, 3])
Tuple!(int[], int[])([1, 2, 3], [])
Tuple!(int[], int[])([], [1, 2, 3])

I cannot understand why the line

Tuple!(int[], int[])([1, 2], [3])

is printed twice at the beginning.

Have I done something wrong in my implementation of opIndex or 
opSlice?


I don't have time to investigate tonight, and it's probably not 
it, but your save isn't saving _upper.


I don't know how your radial works. Does the first back print 
([1, 2, 3], []) as it should?


coding practices: include whole module or only the needed function

2014-10-06 Thread AsmMan via Digitalmars-d-learn
Which practice do you use: if you need only one or two functions 
from a module:


import myModule : func, func2;

or (import whole module, assuming no function name conflits of 
course)


import myModule;

any words why one over the other are welcome.

I like the first one why it explicitly show why I'm importing 
such a module and I think (personally) it make code more easy to 
read/understand/maitain. Also it's very useful inside functions 
(local imports) where we can overload the function like in:


int f(int arg)
{
   import foo : f;

   return f(somethingElse, arg);
}

I used it recently.


Re: slidingSplitter + retro

2014-10-06 Thread Nordlöw

On Monday, 6 October 2014 at 21:15:10 UTC, monarch_dodra wrote:
I don't have time to investigate tonight, and it's probably not 
it, but your save isn't saving _upper.


I don't know how your radial works. Does the first back 
print ([1, 2, 3], []) as it should?


I've added some extra asserts in the last hour.

I believe the problem remaining now lies in the definition of 
opSlice.


I'm currently trying to figure out how it should be defined.

Just uncomment line 263 and 264 and run it.

Thanks anyway, for now!


Re: slidingSplitter + retro

2014-10-06 Thread Nordlöw

On Monday, 6 October 2014 at 21:15:10 UTC, monarch_dodra wrote:
I don't have time to investigate tonight, and it's probably not 
it, but your save isn't saving _upper.


No, that wasn't the current problem.

Thanks for catching it anyway.


Re: slidingSplitter + retro

2014-10-06 Thread Nordlöw

On Monday, 6 October 2014 at 21:15:10 UTC, monarch_dodra wrote:
I don't know how your radial works. Does the first back 
print ([1, 2, 3], []) as it should?


I nailed it!

The problem was the definition of opSlice.

I had to add an extra internal state

lower == upper + 1

to represent SlidingSplitter emptyness.

You're welcome to review again now if you feel like :)

https://github.com/nordlow/justd/blob/master/range_ex.d#L18


Re: coding practices: include whole module or only the needed function

2014-10-06 Thread bearophile via Digitalmars-d-learn

AsmMan:


import myModule : func, func2;


I use this in D to know what I have imported and where is was 
imported from.


Bye,
bearophile


Re: coding practices: include whole module or only the needed function

2014-10-06 Thread H. S. Teoh via Digitalmars-d-learn
On Mon, Oct 06, 2014 at 09:24:54PM +, AsmMan via Digitalmars-d-learn wrote:
 Which practice do you use: if you need only one or two functions from
 a module:
 
 import myModule : func, func2;
 
 or (import whole module, assuming no function name conflits of course)
 
 import myModule;
 
 any words why one over the other are welcome.
 
 I like the first one why it explicitly show why I'm importing such a
 module and I think (personally) it make code more easy to
 read/understand/maitain.

Agreed. Recently, I realized more and more that local imports are the
way to go if you want maintainable code, specifically, easily-
refactorable code.

My old C/C++ habit was to put all imports at the top of the file, but
the problem with that is, when you need to break a source file into
several smaller files, it's a pain to figure out which imports are used
by which pieces of code. It's either that, or copy all the imports
everywhere, which is wasteful when one of the new files doesn't actually
need most of the imports.

Explicitly naming imported symbols is also important, especially given
the recent bugs related to introducing unwanted symbol conflicts into a
scope. It also tells you exactly what symbols a particular piece of code
depends on; for example, mymodule.d may import std.algorithm, but it's
not clear exactly what in std.algorithm is actually used, and where.
Specifically importing std.algorithm : find in func1() and std.algorithm
: nextPermutation in the respective scopes that need it, makes the code
more maintainable -- readers can quickly find out where 'find' and
'nextPermutation' come from without needing to search the docs (or
memorize function names).

Then when you need to move func1() and func2() new files, you know that
newfile1.d only depends on std.algorithm.find, and newfile2.d only
depends on std.algorithm.nextPermutation. So next time you rewrite the
code in func1() and you realize that find() is no longer needed, you can
delete the import statement without worrying that it will break other
parts of the code.

Having said that, though, the *disadvantage* of using scoped explicit
imports is that if you use a lot of symbols from a particular module,
or use the same symbol in many scattered places, then your import
statements could become rather unwieldy and repetitive:

module mymodule;

void func1(...) {
import std.algorithm : find, canFind, nextPermutation,
remove, /* a huge long list here */;
import std.range : isForwardRange;
import std.array : front, empty, popFront;
...
}

void func2(...) {
import std.algorithm : canFind, remove,
cartesianProduct, /* another huge long list
here, repeating many items from previous list
*/;
import std.range : isInputRange;
import std.array : front, empty, popFront;
...
}

So depending on circumstances, sometimes I'd just get lazy and just
use a generic import instead.

Of course, the example above is extreme... normally, you don't need to
use 20 functions from std.algorithm inside a single function; usually
just a small handful is enough. So the above situation should be rather
rare. I also find that in practice, it's my own modules (rather than
Phobos modules) that tend to have symbols that get used repeatedly; in
that case I just use a generic import for that. The one exception is
std.range, which tends to be needed everywhere if you write range-based
code a lot. Usually I just stick `import std.range` at the top of the
file in that case.


 Also it's very useful inside functions (local imports) where we can
 overload the function like in:
 
 int f(int arg)
 {
import foo : f;
 
return f(somethingElse, arg);
 }
 
 I used it recently.

I'm not sure this is a good idea! It makes the code harder to
understand, and if it's a team project, you can bet that one day,
somebody will come and introduce a bug because he/she refactored the
code but failed to notice that the two 'f's refer to two different
things. I'd much rather use the renaming feature of imports:

int f(int arg)
{
import foo : fooF = f;
return fooF(somethingElse, arg);
}


T

-- 
It only takes one twig to burn down a forest.


Re: slidingSplitter + retro

2014-10-06 Thread Nordlöw

On Monday, 6 October 2014 at 22:11:50 UTC, Nordlöw wrote:

https://github.com/nordlow/justd/blob/master/range_ex.d#L18


One more thing: I'm unsure how the indexes lower and upper should 
be treated for narrow strings. Should an index represent a code 
point or a code unit? I'm not sure. In either way I would really 
like this range to just work for narrow strings. What do you say 
monarch_dodra? What's standard D-style way of handling this?