www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Representation length of integral types

reply "tcak" <1ltkrs+3wyh1ow7kzn1k sharklasers.com> writes:
There is a use case for me that I am given a string, and before 
even trying to convert it to integer, I want to check whether it 
is valid. One of the checks necessary for this is the length of 
string.

So, if I have received "156" and it should be converted to ubyte, 
I would check whether it is at most 3 bytes length.

While doing that someone would define that maximum length in the 
code, while length information is constant and never changes.

For this reason, however data types have properties like max, 
min, and init, I would ask for addition of new properties for 
integral (even for floating points as well) types as (Example 
values for ubyte):

max_hex_length = 2
max_dec_length = 3
max_bin_length = 8
Aug 03 2015
next sibling parent reply "rumbu" <rumbu rumbu.ro> writes:
On Tuesday, 4 August 2015 at 04:10:33 UTC, tcak wrote:
 There is a use case for me that I am given a string, and before 
 even trying to convert it to integer, I want to check whether 
 it is valid. One of the checks necessary for this is the length 
 of string.

 So, if I have received "156" and it should be converted to 
 ubyte, I would check whether it is at most 3 bytes length.

 While doing that someone would define that maximum length in 
 the code, while length information is constant and never 
 changes.

 For this reason, however data types have properties like max, 
 min, and init, I would ask for addition of new properties for 
 integral (even for floating points as well) types as (Example 
 values for ubyte):

 max_hex_length = 2
 max_dec_length = 3
 max_bin_length = 8
enum max_hex_length = T.sizeof * 2; enum max_bin_length = T.sizeof * 8; enum max_dec_length = cast(T)log10(T.sizeof) + 1;
Aug 03 2015
parent "rumbu" <rumbu rumbu.ro> writes:
On Tuesday, 4 August 2015 at 06:17:15 UTC, rumbu wrote:

 enum max_hex_length = T.sizeof * 2;
 enum max_bin_length = T.sizeof * 8;
 enum max_dec_length = cast(T)log10(T.sizeof) + 1;
Errata: enum max_dec_length = cast(T)log10(T.max) + 1;
Aug 03 2015
prev sibling parent "Kai Nacke" <kai redstar.de> writes:
On Tuesday, 4 August 2015 at 04:10:33 UTC, tcak wrote:
 max_hex_length = 2
 max_dec_length = 3
 max_bin_length = 8
I think that there is no need to add such properties. They clearly belong into the application domain. min and max values are different because they depend on the internal representation of the number. There should be no problem to use CTFE to calculate these values at compile time. Regards, Kai
Aug 03 2015