Category talk:Wren-str: Difference between revisions

→‎Source code: Adjustments mostly to extend case change methods to Latin-1 (ASCII only previously).
(→‎Source code: Changes to make this module more consistent with other Wren modules.)
(→‎Source code: Adjustments mostly to extend case change methods to Latin-1 (ASCII only previously).)
Line 5:
/*
Char contains routines to perform various operations on characters.
A 'character' for this purpose is a single Unicode codepoint.
For convenience a string containing more than one character can be passed
Categorization and casing is supported for characters < 256 (Latin-1) but no higher.
The 'symbol' category includes 'other letter', 'other number' and soft hyphen (ªº¹²³¼½¾¯).
For convenience a string containing more than one character can be passed
as an argument but the methods will only operate on the first character.
*/
Line 18 ⟶ 21:
// Checks if the first character of a string falls into a particular category.
static isAscii(c) { code(c) < 128 }
static isSymbolisLatin1(c) { code(c) && "$+<=>^`|~".contains(c[0]) 256 }
static isControl(c) { (c = code(c)) && (c < 32 || c == 127) }
static isDigit(c) { (c = code(c)) && c >= 48 && c <= 57 }
static isLower(c) { (c = code(c)) && c >= 97 && c <= 122 }
static isUpper(c) { (c = code(c)) && c >= 65 && c <= 90 }
static isPrintable(c) { (c = code(c)) && c >= 32 && c < 127 }
static isSpace(c) { (c = code(c)) && (c == 32 || c == 9 || c == 10 || c == 13) }
static isWhitespace(c) { (c = code(c)) && (c == 32 || (c >= 9 && c <= 13)) }
 
// ASCII categories.
/* Rather than use combinations of the above, these only call the 'code' method once. */
static isControlisDigit(c) { (c = code(c)) && (c <>= 48 32 ||&& c =<= 127)57 }
static isDigitisAsciiLower(c) { (c = code(c)) && c >= 4897 && c <= 57 122 }
static isLowerisAsciiUpper(c) { (c = code(c)) && c >= 9765 && c <= 122 90 }
static isAsciiLetter(c) { isAsciiLower(c) || isAsciiUpper(c) }
static isAsciiAlphaNum(c) { isAsciiLower(c) || isAsciiUpper(c) || isDigit(c) }
static isUpperisSpace(c) { (c = code(c)) && (c == 32 || c >== 659 &&|| c <== 9010 || c == 13) }
 
// Latin-1 categories.
static isLetterisLower(c) {
var d = code(c)
return (d >= 6597 && d <= 90122) || (d == 181) || (d >= 97223 && d <= 122246) ||
(d >= 248 && d <= 255)
}
 
static isAlphanumericisUpper(c) {
var d = code(c)
return (d >= 65 && d <= 90) || (d >= 97192 && d <= 122214) || (d >= 48216 && d <= 57222)
}
 
static isPunctuationisLetter(c) { isLower(c) || isUpper(c) }
static isAlphaNumeric(c) { isLower(c) || isUpper(c) || isDigit(c) }
 
static isControl(c) {
var d = code(c)
ifreturn (d < 3332 || (d >= 126)127 return&& falsed < 160)
if ((d >= 65 && d <= 90) || (d >= 97 && d <= 122) || (d >= 48 && d <= 57)) return false
if ("$+<=>^`|~".contains(c[0])) return false
return true
}
 
static isPrintable(c) {
var d = code(c)
return (d >= 32 && d < 127) || (d >= 160 && d < 256)
} }
 
static isGraphic(c) {
var d = code(c)
return (d >= 33 && d < 127) || (d >= 161 && d < 256)
} }
 
static isWhitespace(c) {
var d = code(c)
return d == 32 || (d >= 9 && c <= 13) || d == 160
}
static isPunctuation(c) { code(c) && "!\"#\%&'()*,-./:;?@[\\]_{}¡§«¶·»¿".contains(c[0]) }
 
static isSymbol(c) { isGraphic(c) && !isAlpaNumeric(c) && !isPunctuation(c) }
 
static category(c) {
var d = code(c)
return (d < 32) || d == 127) ? "control" :
(d == 32) ? "space" :
(d >= 48 && d <= 57) ? "digit" :
(d >= 64 65 && d <= 90) ? "upper" :
(d >= 97 && d <= 122) ? "lower" :
(d >=128) 127 && d <= 159) ? "control" ? "non-ascii" :
"$+<=>^`|~".contains(c[0]d == 160) ? "symbolspace" : "punctuation" :
(d == 181) ? "lower" :
(d >= 192 && d <= 214) ? "upper" :
(d >= 216 && d <= 222) ? "upper" :
(d >= 223 && d <= 246) ? "lower" :
(d >= 248 && d <= 255) ? "lower" :
(d >= 256) ? "non-latin1" :
isPunctuation(c) ? "punctuation" : "symbol"
}
 
// ReturnReturns the first character of a string converted to the appropriatelower case.
static lower(c) {
static upper(c) { ((c = code(c)) && c >= 97 && c <= 122) ? fromCode(c-32) : fromCode(c) }
var d = code(c)
static lower(c) { ((c = code(c)) && c >= 65 && c <= 90) ? fromCode(c+32) : fromCode(c) }
if ((d >= 65 && d <= 90) || (d >= 97192 && d <= 122214) || (d >= 48216 && d <= 57222)) return false{
return fromCode(d+32)
}
return truec[0]
}
 
// Returns the first character of a string converted to upper case.
static upper(c) {
var d = code(c)
if ((d >= 97 && d <= 122) || (d >= 224 && d <= 246) || (d >= 248 && d <= 254)) {
return fromCode(d-32)
}
return c[0]
}
 
// Swaps the case of the first character in a string.
static swapCase(c) {
var d = code(c)
if ((d >= 65 && d <= 90) return|| fromCode(d+32 >= 192 && d <= 214) || (d >= 216 && d <= 222)) {
if (d >= 97 && d <= 122) return fromCode(d-+32)
}
if ((d >= 97 && d <= 122) || (d >= 224 && d <= 246) || (d >= 248 && d <= 254)) {
return fromCode(d-32)
}
return c[0]
}
Line 95 ⟶ 144:
 
// Checks if a string falls into a particular category.
static allAscii(s) { s.codePoints.all { |c| c < 128 } }
static allDigitsallLatin1(s) { s.codePoints.all { |c| c >= 48 && c <= 57256 } }
static allLowerallDigits(s) { s.codePoints.all { |c| c >= 9748 && c <= 122 57 } }
static allUpperallAsciiLower(s) { s.codePoints.all { |c| c >= 6597 && c <= 90 122 } }
static allPrintableallAsciiUpper(s) { s.codePoints.all { |c| c >= 3265 && c <= 127 90 } }
static allWhitespaceallAsciiLetters(s) { s.codePointstoList.all { |c| c == 32 || Char.isAsciiLetter(c) >= 9 && c <= 13) } }
static allAsciiAlphaNum(s) { s.toList.all { |c| Char.isAsciiAlphaNum(c) } }
 
static allLettersallSpace(s) { s.codePointstoList.all { |c| Char.isSpace(c) } }
static allLower return (c >= 65 && c <= 90) { s.toList.all { |c| Char.isLower(c) >= 97 && c <= 122) } }
static allUpper { s.toList.all { |c| Char.isUpper(c) } }
} }
static allLetters { s.toList.all { |c| Char.isLetter(c) } }
 
static allAlphanumeric(s)allAlphaNumeric { s.codepointstoList.all { |c| Char.isAlphanumeric(c) } }
static allPrintable return (c >= 65 && c{ <=s.toList.all 90){ |c| Char.isPrintable(c >= 97 && c <= 122) || (c >= 48 &&} c <= 57)}
static allGraphic { s.toList.all { |c| Char.isGraphic(c) } }
} }
static allWhitespace { s.toList.all { |c| Char.isWhitespace(c) } }
 
// Checks whether a string can be parsed to a number, an integer or a non-integer (float).
Line 123 ⟶ 173:
var i = 0
for (c in s.codePoints) {
if ((c >= 65 && c <= 90) chars[i]|| (c >= String.fromCodePoint192 && c <= 214) || (c +>= 32216 && c <= 222)) {
chars[i] = String.fromCodePoint(c + 32)
}
i = i + 1
}
Line 137 ⟶ 189:
var i = 0
for (c in s.codePoints) {
if ((c >= 97 && c <= 122) chars[i]|| (c >= String.fromCodePoint224 && c <= 246) || (c ->= 32248 && c <= 254)) {
chars[i] = String.fromCodePoint(c - 32)
}
i = i + 1
}
Line 151 ⟶ 205:
var i = 0
for (c in s.codePoints) {
if ((c >= 65 && c <= 90) || (c >= 192 && c <= 214) || (c >= 216 && c <= 222)) {
chars[i] = String.fromCodePoint(c + 32)
} else if ((c >= 97 && c <= 122) {|| (c >= 224 && c <= 246) ||
(c >= 248 && c <= 254)) {
chars[i] = String.fromCodePoint(c - 32)
}
Line 167 ⟶ 222:
var start = (s.startsWith("[") && s.count > 1) ? 1 : 0
var c = s[start].codePoints[0]
if ((c >= 97 && c <= 122) || (c >= 224 && c <= 246) || (c >= 248 && c <= 254)) {
var cs = String.fromCodePoint(c - 32) + s[start+1..-1]
if (start == 1) cs = "[" + cs
9,476

edits