Functions which operate on ASCII characters.
All of the functions in std.ascii accept Unicode characters but effectively ignore them if they're not ASCII. All isX
functions return false
for non-ASCII characters, and all toX
functions do nothing to non-ASCII characters.
For functions which operate on Unicode characters, see std.uni
.
Category | Functions |
---|---|
Validation | isAlpha isAlphaNum isASCII isControl isDigit isGraphical isHexDigit isOctalDigit isPrintable isPunctuation isUpper isWhite |
Conversions | toLower toUpper |
Constants | digits fullHexDigits hexDigits letters lowercase lowerHexDigits newline octalDigits uppercase whitespace |
Enums | ControlChar LetterCase |
0 .. 9A .. Fa .. f
0 .. 9A .. F
0 .. 9a .. f
0 .. 9
0 .. 7
A .. Za .. z
A .. Z
a .. z
ASCII whitespace
Letter case specifier.
import std.conv : to; writeln(42.to!string(16, LetterCase.upper)); // "2A" writeln(42.to!string(16, LetterCase.lower)); // "2a"
import std.digest.hmac : hmac; import std.digest : toHexString; import std.digest.sha : SHA1; import std.string : representation; const sha1HMAC = "A very long phrase".representation .hmac!SHA1("secret".representation) .toHexString!(LetterCase.lower); writeln(sha1HMAC); // "49f2073c7bf58577e8c9ae59fe8cfd37c9ab94e5"
Upper case letters
Lower case letters
All control characters in the ASCII table (source).
import std.algorithm.comparison, std.algorithm.searching, std.range, std.traits; // Because all ASCII characters fit in char, so do these static assert(ControlChar.ack.sizeof == 1); // All control characters except del are in row starting from 0 static assert(EnumMembers!ControlChar.only.until(ControlChar.del).equal(iota(32))); static assert(ControlChar.nul == '\0'); static assert(ControlChar.bel == '\a'); static assert(ControlChar.bs == '\b'); static assert(ControlChar.ff == '\f'); static assert(ControlChar.lf == '\n'); static assert(ControlChar.cr == '\r'); static assert(ControlChar.tab == '\t'); static assert(ControlChar.vt == '\v');
import std.conv; //Control character table can be used in place of hexcodes. with (ControlChar) assert(text("Phobos", us, "Deimos", us, "Tango", rs) == "Phobos\x1FDeimos\x1FTango\x1E");
Null
Start of heading
Start of text
End of text
End of transmission
Enquiry
Acknowledge
Bell
Backspace
Horizontal tab
NL line feed, new line
Vertical tab
NP form feed, new page
Carriage return
Shift out
Shift in
Data link escape
Device control 1
Device control 2
Device control 3
Device control 4
Negative acknowledge
Synchronous idle
End of transmission block
Cancel
End of medium
Substitute
Escape
File separator
Group separator
Record separator
Unit separator
Delete
Newline sequence for this system.
dchar c
| The character to test. |
c
is a letter or a number (0 .. 9, a .. z, A .. Z).assert( isAlphaNum('A')); assert( isAlphaNum('1')); assert(!isAlphaNum('#')); // N.B.: does not return true for non-ASCII Unicode alphanumerics: assert(!isAlphaNum('á'));
dchar c
| The character to test. |
c
is an ASCII letter (A .. Z, a .. z).assert( isAlpha('A')); assert(!isAlpha('1')); assert(!isAlpha('#')); // N.B.: does not return true for non-ASCII Unicode alphabetic characters: assert(!isAlpha('á'));
dchar c
| The character to test. |
c
is a lowercase ASCII letter (a .. z).assert( isLower('a')); assert(!isLower('A')); assert(!isLower('#')); // N.B.: does not return true for non-ASCII Unicode lowercase letters assert(!isLower('á')); assert(!isLower('Á'));
dchar c
| The character to test. |
c
is an uppercase ASCII letter (A .. Z).assert( isUpper('A')); assert(!isUpper('a')); assert(!isUpper('#')); // N.B.: does not return true for non-ASCII Unicode uppercase letters assert(!isUpper('á')); assert(!isUpper('Á'));
dchar c
| The character to test. |
c
is a digit (0 .. 9).assert( isDigit('3')); assert( isDigit('8')); assert(!isDigit('B')); assert(!isDigit('#')); // N.B.: does not return true for non-ASCII Unicode numbers assert(!isDigit('0')); // full-width digit zero (U+FF10) assert(!isDigit('4')); // full-width digit four (U+FF14)
dchar c
| The character to test. |
c
is a digit in base 8 (0 .. 7).assert( isOctalDigit('0')); assert( isOctalDigit('7')); assert(!isOctalDigit('8')); assert(!isOctalDigit('A')); assert(!isOctalDigit('#'));
dchar c
| The character to test. |
c
is a digit in base 16 (0 .. 9, A .. F, a .. f).assert( isHexDigit('0')); assert( isHexDigit('A')); assert( isHexDigit('f')); // lowercase hex digits are accepted assert(!isHexDigit('g')); assert(!isHexDigit('G')); assert(!isHexDigit('#'));
dchar c
| The character to test. |
c
is a whitespace character. That includes the space, tab, vertical tab, form feed, carriage return, and linefeed characters.assert( isWhite(' ')); assert( isWhite('\t')); assert( isWhite('\n')); assert(!isWhite('1')); assert(!isWhite('a')); assert(!isWhite('#')); // N.B.: Does not return true for non-ASCII Unicode whitespace characters. static import std.uni; assert(std.uni.isWhite('\u00A0')); assert(!isWhite('\u00A0')); // std.ascii.isWhite
dchar c
| The character to test. |
c
is a control character.assert( isControl('\0')); assert( isControl('\022')); assert( isControl('\n')); // newline is both whitespace and control assert(!isControl(' ')); assert(!isControl('1')); assert(!isControl('a')); assert(!isControl('#')); // N.B.: non-ASCII Unicode control characters are not recognized: assert(!isControl('\u0080')); assert(!isControl('\u2028')); assert(!isControl('\u2029'));
dchar c
| The character to test. |
c
is a punctuation character. That includes all ASCII characters which are not control characters, letters, digits, or whitespace.assert( isPunctuation('.')); assert( isPunctuation(',')); assert( isPunctuation(':')); assert( isPunctuation('!')); assert( isPunctuation('#')); assert( isPunctuation('~')); assert( isPunctuation('+')); assert( isPunctuation('_')); assert(!isPunctuation('1')); assert(!isPunctuation('a')); assert(!isPunctuation(' ')); assert(!isPunctuation('\n')); assert(!isPunctuation('\0')); // N.B.: Non-ASCII Unicode punctuation characters are not recognized. assert(!isPunctuation('\u2012')); // (U+2012 = en-dash)
dchar c
| The character to test. |
c
is a printable character other than the space character.assert( isGraphical('1')); assert( isGraphical('a')); assert( isGraphical('#')); assert(!isGraphical(' ')); // whitespace is not graphical assert(!isGraphical('\n')); assert(!isGraphical('\0')); // N.B.: Unicode graphical characters are not regarded as such. assert(!isGraphical('á'));
dchar c
| The character to test. |
c
is a printable character - including the space character.assert( isPrintable(' ')); // whitespace is printable assert( isPrintable('1')); assert( isPrintable('a')); assert( isPrintable('#')); assert(!isPrintable('\0')); // control characters are not printable // N.B.: Printable non-ASCII Unicode characters are not recognized. assert(!isPrintable('á'));
dchar c
| The character to test. |
c
is in the ASCII character set - i.e. in the range 0 .. 0x7F.assert( isASCII('a')); assert(!isASCII('á'));
Converts an ASCII letter to lowercase.
C c
| A character of any type that implicitly converts to dchar . In the case where it's a built-in type, or an enum of a built-in type, Unqual!(OriginalType!C) is returned, whereas if it's a user-defined type, dchar is returned. |
c
is an uppercase ASCII character, otherwise c
itself.writeln(toLower('a')); // 'a' writeln(toLower('A')); // 'a' writeln(toLower('#')); // '#' // N.B.: Non-ASCII Unicode uppercase letters are not converted. writeln(toLower('Á')); // 'Á'
Converts an ASCII letter to uppercase.
C c
| Any type which implicitly converts to dchar . In the case where it's a built-in type, or an enum of a built-in type, Unqual!(OriginalType!C) is returned, whereas if it's a user-defined type, dchar is returned. |
c
is a lowercase ASCII character, otherwise c
itself.writeln(toUpper('a')); // 'A' writeln(toUpper('A')); // 'A' writeln(toUpper('#')); // '#' // N.B.: Non-ASCII Unicode lowercase letters are not converted. writeln(toUpper('á')); // 'á'
© 1999–2019 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_ascii.html