digitalmars.D - MAX_CHAR
- Ben Hanson (6/6) Jun 19 2010 Hi there,
- Justin Johansson (13/22) Jun 19 2010 Ben, are you talking about a lexical value space or a value value space?
- Ben Hanson (54/61) Jun 19 2010 I am converting some template C++ code that can work with char or wchar_...
- BCS (24/34) Jun 19 2010 If you are looking for the X sutch that the following is true (as long a...
- Ben Hanson (8/40) Jun 19 2010 and
- Andrei Alexandrescu (16/22) Jun 19 2010 Something like:
- Ben Hanson (7/32) Jun 19 2010 Hi Andrei,
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
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, BenBen, 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
Hi Justin, == Quote from Justin Johansson (no spam.com)'s articleBen, 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.I am converting some template C++ code that can work with char or wchar_t. This code does 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
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
Hi BCS, == Quote from BCS (none anon.com)'s articleHello Ben,andHi 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 wstringlong asneed to determine the character type from the string type.If you are looking for the X sutch that the following is true (asfn 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'Yep, that's it! Thanks a lot BenThanks, Ben
Jun 19 2010
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, BenSomething 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
Hi Andrei, == Quote from Andrei Alexandrescu (SeeWebsiteForEmail erdani.org)'s articleOn 06/19/2010 08:47 AM, Ben Hanson wrote:andHi 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 wstringGreat, this is even easier. Thanks, Benneed to determine the character type from the string type. Thanks, BenSomething 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