Fix {str|bytes|bytearray}.{isascii,islower,isupper}

This commit is contained in:
Jeong YunWon
2020-04-11 10:59:26 +09:00
parent c7467e444d
commit 95e938cb9b
3 changed files with 39 additions and 27 deletions

View File

@@ -611,7 +611,7 @@ impl PyByteInner {
}
pub fn isascii(&self) -> bool {
!self.elements.is_empty() && self.elements.iter().all(|x| char::from(*x).is_ascii())
self.elements.iter().all(|x| char::from(*x).is_ascii())
}
pub fn isdigit(&self) -> bool {
@@ -619,25 +619,39 @@ impl PyByteInner {
}
pub fn islower(&self) -> bool {
!self.elements.is_empty()
&& self
.elements
.iter()
.filter(|x| !char::from(**x).is_whitespace())
.all(|x| char::from(*x).is_lowercase())
}
pub fn isspace(&self) -> bool {
!self.elements.is_empty() && self.elements.iter().all(|x| char::from(*x).is_whitespace())
// CPython _Py_bytes_islower
let mut cased = false;
for b in self.elements.iter() {
let c = *b as char;
if c.is_uppercase() {
return false;
} else if !cased && c.is_lowercase() {
cased = true
}
}
cased
}
pub fn isupper(&self) -> bool {
// CPython _Py_bytes_isupper
let mut cased = false;
for b in self.elements.iter() {
let c = *b as char;
if c.is_lowercase() {
return false;
} else if !cased && c.is_uppercase() {
cased = true
}
}
cased
}
pub fn isspace(&self) -> bool {
!self.elements.is_empty()
&& self
.elements
.iter()
.filter(|x| !char::from(**x).is_whitespace())
.all(|x| char::from(*x).is_uppercase())
.all(|x| char::from(*x).is_ascii_whitespace())
}
pub fn istitle(&self) -> bool {

View File

@@ -9,7 +9,6 @@ use std::string::ToString;
use num_traits::ToPrimitive;
use unic::ucd::category::GeneralCategory;
use unic::ucd::ident::{is_xid_continue, is_xid_start};
use unic::ucd::is_cased;
use unicode_casing::CharExt;
use super::objbytes::{PyBytes, PyBytesRef};
@@ -826,29 +825,31 @@ impl PyString {
!self.value.is_empty() && self.value.chars().all(|c| c.is_ascii_whitespace())
}
// Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
// Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
#[pymethod]
fn isupper(&self) -> bool {
fn islower(&self) -> bool {
// CPython unicode_islower_impl
let mut cased = false;
for c in self.value.chars() {
if is_cased(c) && c.is_uppercase() {
cased = true
} else if is_cased(c) && c.is_lowercase() {
if c.is_uppercase() {
return false;
} else if !cased && c.is_lowercase() {
cased = true
}
}
cased
}
// Return true if all cased characters in the string are lowercase and there is at least one cased character, false otherwise.
// Return true if all cased characters in the string are uppercase and there is at least one cased character, false otherwise.
#[pymethod]
fn islower(&self) -> bool {
fn isupper(&self) -> bool {
// CPython unicode_isupper_impl
let mut cased = false;
for c in self.value.chars() {
if is_cased(c) && c.is_lowercase() {
cased = true
} else if is_cased(c) && c.is_uppercase() {
if c.is_lowercase() {
return false;
} else if !cased && c.is_uppercase() {
cased = true
}
}
cased