Skip to content

Commit

Permalink
feat!: Make an ascii submodule
Browse files Browse the repository at this point in the history
  • Loading branch information
spotandjake committed Oct 28, 2024
1 parent 2ce457d commit 466913c
Show file tree
Hide file tree
Showing 5 changed files with 445 additions and 225 deletions.
118 changes: 70 additions & 48 deletions compiler/test/stdlib/char.test.gr
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,73 @@ assert !('a' >= 'b')
assert 'a' >= 'a'
assert 'B' >= 'B'

// isAscii
assert Char.isAscii('1')
assert Char.isAscii('a')
assert Char.isAscii(';')
assert Char.isAscii(' ')
assert Char.isAscii('\n')
assert !Char.isAscii('🌾')

// isAsciiDigit
assert Char.isAsciiDigit('1')
assert !Char.isAsciiDigit('a')
assert !Char.isAsciiDigit('🌾')

// isAsciiAlpha
assert !Char.isAsciiAlpha('1')
assert Char.isAsciiAlpha('a')
assert Char.isAsciiAlpha('Z')
assert !Char.isAsciiAlpha('λ')

// isAsciiControl
assert Char.isAsciiControl('\n')
assert Char.isAsciiControl('\t')
assert Char.isAsciiControl('\u{007F}')
assert !Char.isAsciiControl(' ')
assert !Char.isAsciiControl('a')
assert !Char.isAsciiControl('🌾')

// isAsciiWhitespace
assert Char.isAsciiWhitespace(' ')
assert Char.isAsciiWhitespace('\t')
assert Char.isAsciiWhitespace('\n')
assert Char.isAsciiWhitespace('\r')
assert Char.isAsciiWhitespace('\x0C')
assert !Char.isAsciiWhitespace('a')
assert !Char.isAsciiWhitespace('1')
assert !Char.isAsciiWhitespace('🌾')

// toAsciiLowercase
assert Char.toAsciiLowercase('A') == 'a'
assert Char.toAsciiLowercase('a') == 'a'
assert Char.toAsciiLowercase('1') == '1'
assert Char.toAsciiLowercase('λ') == 'λ'

// toAsciiUppercase
assert Char.toAsciiUppercase('a') == 'A'
assert Char.toAsciiUppercase('A') == 'A'
assert Char.toAsciiUppercase('1') == '1'
assert Char.toAsciiUppercase('λ') == 'λ'
// Char.Ascii
module AsciiTest {
use Char.{ module Ascii }

// isValid
assert Ascii.isValid('1')
assert Ascii.isValid('a')
assert Ascii.isValid(';')
assert Ascii.isValid(' ')
assert Ascii.isValid('\n')
assert !Ascii.isValid('🌾')

// isDigit
assert Ascii.isDigit('1')
assert !Ascii.isDigit('a')
assert !Ascii.isDigit('🌾')

// isAlpha
assert !Ascii.isAlpha('1')
assert Ascii.isAlpha('a')
assert Ascii.isAlpha('Z')
assert !Ascii.isAlpha('λ')

// isControl
assert Ascii.isControl('\n')
assert Ascii.isControl('\t')
assert Ascii.isControl('\u{007F}')
assert !Ascii.isControl(' ')
assert !Ascii.isControl('a')
assert !Ascii.isControl('🌾')

// isWhitespace
assert Ascii.isWhitespace(' ')
assert Ascii.isWhitespace('\t')
assert Ascii.isWhitespace('\n')
assert Ascii.isWhitespace('\r')
assert Ascii.isWhitespace('\x0C')
assert !Ascii.isWhitespace('a')
assert !Ascii.isWhitespace('1')
assert !Ascii.isWhitespace('🌾')

// isPunctuation
assert Ascii.isPunctuation('!')
assert Ascii.isPunctuation('?')
assert Ascii.isPunctuation('.')
assert Ascii.isPunctuation(',')
assert !Ascii.isPunctuation('1')
assert !Ascii.isPunctuation('a')
assert !Ascii.isPunctuation('🌾')

// isGraphic
assert Ascii.isGraphic('1')
assert Ascii.isGraphic('a')
assert Ascii.isGraphic('!')
assert !Ascii.isGraphic('\n')
assert !Ascii.isGraphic('\t')
assert !Ascii.isGraphic('🌾')

// toLowercase
assert Ascii.toLowercase('A') == 'a'
assert Ascii.toLowercase('a') == 'a'
assert Ascii.toLowercase('1') == '1'
assert Ascii.toLowercase('λ') == 'λ'

// toUppercase
assert Ascii.toUppercase('a') == 'A'
assert Ascii.toUppercase('A') == 'A'
assert Ascii.toUppercase('1') == '1'
assert Ascii.toUppercase('λ') == 'λ'
}
249 changes: 153 additions & 96 deletions stdlib/char.gr
Original file line number Diff line number Diff line change
Expand Up @@ -315,113 +315,170 @@ provide let (>=) = (x: Char, y: Char) => {
}

/**
* Checks if the character is an ASCII character.
* Sub module for working with ASCII characters.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII character or `false` otherwise
*
* @example assert Char.isAscii('1')
* @example assert Char.isAscii('a')
* @example assert !Char.isAscii('🌾')
* @example Char.Ascii.isAscii('1')
*
* @since v0.7.0
*/
provide let isAscii = char => char <= '\u{007F}' // usv <= 0x7F
provide module Ascii {
/**
* The minimum valid ASCII character code.
*
* @since v0.7.0
*/
provide let min = 0x00

/**
* Checks if the character is an ASCII digit.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII digit or `false` otherwise
*
* @example assert Char.isAsciiDigit('1')
* @example assert !Char.isAsciiDigit('a')
*
* @since v0.6.0
*/
provide let isAsciiDigit = char => char >= '0' && char <= '9'
/**
* The maximum valid ASCII character code.
*
* @since v0.7.0
*/
provide let max = 0x7F

/**
* Checks if the character is an ASCII alphabetical character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII alphabetical or `false` otherwise
*
* @example assert Char.isAsciiAlpha('a')
* @example assert !Char.isAsciiAlpha('1')
*
* @since v0.6.0
*/
provide let isAsciiAlpha = char =>
char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z'
/**
* Checks if the character is a valid ASCII character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII character or `false` otherwise
*
* @example assert Char.Ascii.isValid('1')
* @example assert Char.Ascii.isValid('a')
* @example assert !Char.Ascii.isValid('🌾')
*
* @since v0.7.0
*/
provide let isValid = char => char <= '\u{007F}' // usv <= 0x7F

/**
* Checks if the character is an ASCII control character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII control character or `false` otherwise
*
* @example assert Char.isAsciiControl('\t')
* @example assert Char.isAsciiControl('\n')
* @example assert !Char.isAsciiControl('1')
* @example assert !Char.isAsciiControl('a')
*
* @since v0.7.0
*/
provide let isAsciiControl = char => char <= '\u{001F}' || char == '\u{007F}'
/**
* Checks if the character is an ASCII digit.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII digit or `false` otherwise
*
* @example assert Char.Ascii.isDigit('1')
* @example assert !Char.Ascii.isDigit('a')
*
* @since v0.7.0
* @history v0.6.0: Originally `Char.isAsciiDigit`
*/
provide let isDigit = char => char >= '0' && char <= '9'

/**
* Checks if the character is an ASCII whitespace character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII whitespace character or `false` otherwise
*
* @example assert Char.isAsciiWhitespace('\t')
* @example assert Char.isAsciiWhitespace('\n')
* @example assert !Char.isAsciiWhitespace('1')
* @example assert !Char.isAsciiWhitespace('a')
*
* @since v0.7.0
*/
provide let isAsciiWhitespace = char => {
match (char) {
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
_ => false,
/**
* Checks if the character is an ASCII alphabetical character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII alphabetical or `false` otherwise
*
* @example assert Char.Ascii.isAlpha('a')
* @example assert !Char.Ascii.isAlpha('1')
*
* @since v0.7.0
* @history v0.6.0: Originally `Char.isAsciiAlpha`
*/
provide let isAlpha = char =>
char >= 'a' && char <= 'z' || char >= 'A' && char <= 'Z'

/**
* Checks if the character is an ASCII control character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII control character or `false` otherwise
*
* @example assert Char.Ascii.isControl('\t')
* @example assert Char.Ascii.isControl('\n')
* @example assert !Char.Ascii.isControl('1')
* @example assert !Char.Ascii.isControl('a')
*
* @since v0.7.0
*/
provide let isControl = char => char <= '\u{001F}' || char == '\u{007F}'

/**
* Checks if the character is an ASCII whitespace character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII whitespace character or `false` otherwise
*
* @example assert Char.isWhitespace('\t')
* @example assert Char.isWhitespace('\n')
* @example assert !Char.isWhitespace('1')
* @example assert !Char.isWhitespace('a')
*
* @since v0.7.0
*/
provide let isWhitespace = char => {
match (char) {
'\t' | '\n' | '\x0C' | '\r' | ' ' => true,
_ => false,
}
}
}

/**
* Converts the character to ASCII lowercase if it is an ASCII uppercase character.
*
* @param char: The character to convert
* @returns The lowercased character
*
* @example assert Char.toAsciiLowercase('B') == 'b'
*
* @since v0.6.0
*/
provide let toAsciiLowercase = char => {
if (char >= 'A' && char <= 'Z') {
fromCode(code(char) + 0x20)
} else {
char
/**
* Checks if the character is an ASCII punctuation character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII punctuation character or `false` otherwise
*
* @example assert Char.Ascii.isPunctuation('!')
* @example assert !Char.Ascii.isPunctuation('1')
*
* @since v0.7.0
*/
provide let isPunctuation = char =>
char >= '!' && char <= '/' ||
char >= ':' && char <= '@' ||
char >= '[' && char <= '`' ||
char >= '{' && char <= '~'

/**
* Checks if the character is an ASCII graphic character.
*
* @param char: The character to check
* @returns `true` if the character is an ASCII graphic character or `false` otherwise
*
* @example assert Char.Ascii.isGraphic('!')
* @example assert !Char.Ascii.isGraphic('\t')
*
* @since v0.7.0
*/
provide let isGraphic = char => char >= '!' && char <= '~'

/**
* Converts the character to ASCII lowercase if it is an ASCII uppercase character.
*
* @param char: The character to convert
* @returns The lowercased character
*
* @example assert Char.Ascii.toLowercase('B') == 'b'
*
* @since v0.7.0
* @history v0.6.0: Originally `Char.toAsciiLowercase`
*/
provide let toLowercase = char => {
if (char >= 'A' && char <= 'Z') {
fromCode(code(char) + 0x20)
} else {
char
}
}
}

/**
* Converts the character to ASCII uppercase if it is an ASCII lowercase character.
*
* @param char: The character to convert
* @returns The uppercased character
*
* @example assert Char.toAsciiUppercase('b') == 'B'
*
* @since v0.6.0
*/
provide let toAsciiUppercase = char => {
if (char >= 'a' && char <= 'z') {
fromCode(code(char) - 0x20)
} else {
char
/**
* Converts the character to ASCII uppercase if it is an ASCII lowercase character.
*
* @param char: The character to convert
* @returns The uppercased character
*
* @example assert Char.Ascii.toUppercase('b') == 'B'
*
* @since v0.7.0
* @history v0.6.0: Originally `Char.toAsciiUppercase`
*/
provide let toUppercase = char => {
if (char >= 'a' && char <= 'z') {
fromCode(code(char) - 0x20)
} else {
char
}
}
}
Loading

0 comments on commit 466913c

Please sign in to comment.