www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - MAX_CHAR

reply Ben Hanson <Ben.Hanson tfbplc.co.uk> writes:
Hi there,

Can anyone tell me how to get the maximum char value from a string
type? I have a template struct which can take string or wstring and
need to determine the character type from the string type.

Thanks,

Ben
Jun 19 2010
next sibling parent reply Justin Johansson <no spam.com> writes:
Ben Hanson wrote:
 Hi there,
 
 Can anyone tell me how to get the maximum char value from a string
 type? I have a template struct which can take string or wstring and
 need to determine the character type from the string type.
 
 Thanks,
 
 Ben

Ben, are you talking about a lexical value space or a value value space? It might help you to solicit a better answer if you posted some code such as your template struct, and perhaps some method function signatures. It would also help if you explained what you mean by a character type as opposed to a string type. Are you asking about character encodings such as UTF-8 or UTF-16 or US-ASCII for example? Sorry it is not clear in your question. On the other hand, are you asking a question about the D type system because, sorry AFAIK, D does not have a type system that is amenable to formal analysis and in this case any answers would be meaningless. Cheers Justin Johansson
Jun 19 2010
parent Ben Hanson <Ben.Hanson tfbplc.co.uk> writes:
Hi Justin,

== Quote from Justin Johansson (no spam.com)'s article
 Ben, are you talking about a lexical value space or a value value space?
 It might help you to solicit a better answer if you posted some code
 such as your template struct, and perhaps some method function signatures.
 It would also help if you explained what you mean by a character type as
 opposed to a string type.  Are you asking about character encodings such
 as UTF-8 or UTF-16 or US-ASCII for example?  Sorry it is not clear in
 your question.

not cope with any UTF encoding (I realise this support will ultimately need adding!) Apparently in D you can do char.max and all I would like to be able to do is that but from a string type. Here is the code I have so far: module main; import std.algorithm; import std.string; template regex(StringT) { struct basic_string_token { bool _negated = false; StringT _charset; this(const bool negated_, ref StringT charset_) { _negated = negated_; _charset = charset_; } void remove_duplicates() { _charset.sort; _charset = squeeze(_charset); } void normalise() { const size_t max_chars_ = 256; // Needs conditional here based on char size if (_charset.length == max_chars_) { _negated = !_negated; _charset.clear(); } else if (_charset.length > max_chars_ / 2) { negate(); } } void negate() { _negated = !_negated; } }; } int main(char[][]argv) { regex!(string).basic_string_token token_; token_._charset = "cccbba"; token_.remove_duplicates(); return 0; } It's in the normalise() routine I would like to know the size of 'char' in. Thanks, Ben
Jun 19 2010
prev sibling next sibling parent reply BCS <none anon.com> writes:
Hello Ben,

 Hi there,
 
 Can anyone tell me how to get the maximum char value from a string
 type? I have a template struct which can take string or wstring and
 need to determine the character type from the string type.
 

If you are looking for the X sutch that the following is true (as long as fn returns a valid string) someStringType foo = fn(); assert(foo[rand()%$] <= X); // check a random position. Bits out of the following should work: template Max(T) { static if(is(T U : U[])) const string Max = U.stringof ~ ":" ~ (cast(int)(U.max)).stringof; else static assert(false, "oops"); } import std.stdio; void main() { writef("%s\n", Max!(string)); writef("%s\n", Max!(wchar[])); writef("%s\n", Max!(dchar[])); } Output: char:cast(int)'\xff' wchar:cast(int)'\U0000ffff' dchar:cast(int)'\U0010ffff'
 Thanks,
 
 Ben
 

... <IXOYE><
Jun 19 2010
parent Ben Hanson <Ben.Hanson tfbplc.co.uk> writes:
Hi BCS,

== Quote from BCS (none anon.com)'s article
 Hello Ben,
 Hi there,

 Can anyone tell me how to get the maximum char value from a string
 type? I have a template struct which can take string or wstring


 need to determine the character type from the string type.


 fn returns a valid string)
 someStringType foo = fn();
 assert(foo[rand()%$] <= X);  // check a random position.
 Bits out of the following should work:
 template Max(T)  {
   static if(is(T U : U[]))
     const string Max = U.stringof ~ ":" ~ (cast(int)

   else static assert(false, "oops");
 }
 import std.stdio;
 void main()
 {
    writef("%s\n", Max!(string));
    writef("%s\n", Max!(wchar[]));
    writef("%s\n", Max!(dchar[]));
 }
 Output:
   char:cast(int)'\xff'
   wchar:cast(int)'\U0000ffff'
   dchar:cast(int)'\U0010ffff'
 Thanks,

 Ben


Yep, that's it! Thanks a lot Ben
Jun 19 2010
prev sibling parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 06/19/2010 08:47 AM, Ben Hanson wrote:
 Hi there,

 Can anyone tell me how to get the maximum char value from a string
 type? I have a template struct which can take string or wstring and
 need to determine the character type from the string type.

 Thanks,

 Ben

Something like: import std.stdio; struct Widget(S) { enum uint MAX_CHAR = typeof(S.init[0]).max; } void main() { writeln(Widget!string.MAX_CHAR); writeln(Widget!wstring.MAX_CHAR); writeln(Widget!dstring.MAX_CHAR); } writes 255 65535 1114111 Andrei
Jun 19 2010
parent Ben Hanson <Ben.Hanson tfbplc.co.uk> writes:
Hi Andrei,

== Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s
article
 On 06/19/2010 08:47 AM, Ben Hanson wrote:
 Hi there,

 Can anyone tell me how to get the maximum char value from a string
 type? I have a template struct which can take string or wstring


 need to determine the character type from the string type.

 Thanks,

 Ben

import std.stdio; struct Widget(S) { enum uint MAX_CHAR = typeof(S.init[0]).max; } void main() { writeln(Widget!string.MAX_CHAR); writeln(Widget!wstring.MAX_CHAR); writeln(Widget!dstring.MAX_CHAR); } writes 255 65535 1114111 Andrei

Great, this is even easier. Thanks, Ben
Jun 19 2010