Any elegant way to define an alphanumeric subtype?

This is my first question on this site. I always managed to find the answer to the question from some people with a similar problem than mine, but this time they did not seem to be there.

So, here I am trying to create a large number of relatively short lines for use as identification numbers, but I want them to contain only alphanumeric characters.

I tried some things like:

subtype Char is character range 'a' .. 'z' | 'A' .. 'Z' | '0' .. '9';

Adding brackets, commas, creating a new type instead of a subtype.

I know that I can just list all the characters that I want, but I just could put up with such a stupid task as writing every 62 characters in an enumerated type.

Is there a way to concatenate different intervals into one subtype?

EDIT: so this is my code:

with ada.text_io, ada.integer_text_io, ada.float_text_io, ada.numerics.discrete_random;
use  ada.text_io, ada.integer_text_io, ada.float_text_io;

procedure randomID is

Subtype AlphaNumeric is character 
with dynamic_predicate => AlphaNumeric in 'a'..'z' |
                                          'A'..'Z' |
                                          '1'..'9' ;
package CharGen is new ada.numerics.discrete_random (Alphanumeric);
CharG: CharGen.generator;
id: string (1..5);

begin
    for i in 1..5 loop
        CharGen.Reset(CharG);
        id(i) := charGen.random(CharG);
    end loop;
end randomID;

( ) :

09: : a-nudira.adb: 54
09: : "Result_Subtype" , ""
09: : Program_Error

09: : a-nudira.adb: 54
09: : "Result_Subtype"
09: : s-rannum.ads:86
09: : a-nudira.adb: 53
09: : "Result_Subtype" , ""
09: : s-rannum.adb: 395
09: : a-nudira.adb: 53
09: : "Result_Subtype" , ""

, , Ada.numerics . ?

+4
3

, , .

Character :

subtype Alphanumeric_Character is Character
  with Static_Predicate => Alphanumeric_Character in 'a' .. 'z' |
                                                     'A' .. 'Z' |
                                                     '0' .. '9';

:

type Alphanumeric_Character is ('a', 'A',
                                'b', 'B',
                                'c', 'C',
                                ...
                                'z', 'Z',
                                '0', '1', '2', '3', '4',
                                '5', '6', '7', '8', '9');
+3

, , , 2012 .

, , , , ...

, , 'first, 'last, 'range, , a-nudira.adb aka ada.numerics.discrete_random. 'first_valid, 'last_valid, , 'range, .

, 'pos, 'val, 'pred,'succ, , , AlphaNumeric'Pred('A') - "@", "9", . , ...

, - , .., .

, ( , ) .

, ada.numerics.discrete_random . , , , , . (OTOH, Gnat Pro, : -)

, , random, ...

with ada.text_io, ada.integer_text_io, ada.float_text_io, 
ada.numerics.discrete_random;
use  ada.text_io, ada.integer_text_io, ada.float_text_io;

procedure randomID is

pragma Assertion_Policy(Check);

Subtype AlphaNumeric is character 
with dynamic_predicate => AlphaNumeric in 'a'..'z' |
                                          'A'..'Z' |
                                          '1'..'9' ;

package CharGen is new ada.numerics.discrete_random (character);
CharG: CharGen.generator;

id : string (1..5);

    function random return AlphaNumeric is
    ch : character;
    begin
        loop
           ch := charGen.random(CharG);
           exit when ch in AlphaNumeric;
        end loop;
        return ch;
    end random;

begin
    for i in 1..5 loop
        CharGen.Reset(CharG); -- wait what? should be outside the loop
        id(i) := random;
    end loop;
    put_line(id);
end randomID;

, , . , , - , . , id a array (1 .. 5) of AlphaNumeric, , .

+1

, . , , . , , , , , -.

procedure randomID is

Type Table is array (1..62) of character;
AlphaNumeric : Table := ('a','b','c'...'7','8','9');
Type Int is new integer range 1..62;
package intGen is new ada.numerics.discrete_random (Int);
intG: IntGen.generator;
id: string (1..5);

begin
    for i in 1..5 loop
        intGen.Reset(intG);
        id(i) := Alphanumeric(integer(intGen.Random(intG)));
    end loop;
end randomID;
0

Source: https://habr.com/ru/post/1690101/


All Articles