pystr::get_chars

This commit is contained in:
Jeong YunWon committed 2020-04-29 03:05:01 +09:00
1 parent 5899348bdb
commit a3eb9a33da
3 files changed
+21 -2

No files matched your search

+4
View File
@@ -1324,6 +1324,10 @@ impl PyCommonString<u8> for [u8] {
&self[range]
}
fn get_chars<'a>(&'a self, range: std::ops::Range<usize>) -> &'a Self {
&self[range]
}
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
+13
View File
@@ -1745,6 +1745,19 @@ impl PyCommonString<char> for str {
&self[range]
}
fn get_chars<'a>(&'a self, range: std::ops::Range<usize>) -> &'a Self {
let mut chars = self.chars();
for _ in 0..range.start {
let _ = chars.next();
}
let start = chars.as_str();
for _ in range {
let _ = chars.next();
}
let end = chars.as_str();
&start[..start.len() - end.len()]
}
fn is_empty(&self) -> bool {
Self::is_empty(self)
}
+4 -2
View File
@@ -118,6 +118,8 @@ pub trait PyCommonString<E> {
fn with_capacity(capacity: usize) -> Self::Container;
fn get_bytes<'a>(&'a self, range: std::ops::Range<usize>) -> &'a Self;
// FIXME: get_chars is expensive for str
fn get_chars<'a>(&'a self, range: std::ops::Range<usize>) -> &'a Self;
fn bytes_len(&self) -> usize;
fn chars_len(&self) -> usize;
fn is_empty(&self) -> bool;
@@ -216,7 +218,7 @@ pub trait PyCommonString<E> {
{
if range.is_normal() {
let start = range.start;
if let Some(index) = find(self.get_bytes(range), &needle) {
if let Some(index) = find(self.get_chars(range), &needle) {
return Some(start + index);
}
}
@@ -229,7 +231,7 @@ pub trait PyCommonString<E> {
F: Fn(&Self, &Self) -> usize,
{
if range.is_normal() {
count(self.get_bytes(range), &needle)
count(self.get_chars(range), &needle)
} else {
0
}