Add strip_str_prefix function
MODIFY
binding-generator/src/class.rs
binding-generator/src/class.rs
@@ -392,9 +392,8 @@ impl<'tu, 'g> Class<'tu, 'g> {
let mut typ = type_ref.rust_full();
// hack for converting the references to array types in struct definitions
if type_ref.as_fixed_array().is_some() {
- const STRIP: &str = "&mut ";
- if typ.starts_with(STRIP) {
- typ = typ[STRIP.len()..].to_string().into()
+ if let Some(new_typ) = typ.strip_str_prefix("&mut ") {
+ typ = new_typ.to_string().into()
}
}
SIMPLE_FIELD_TPL.interpolate(&hashmap! {

MODIFY
binding-generator/src/comment.rs
binding-generator/src/comment.rs
@@ -26,8 +26,8 @@ pub fn strip_comment_markers(comment: &str) -> String {
if line_clean.starts_with(MULTILINE_PREFIX) {
singleline_delimited = false;
line = &line_clean[MULTILINE_PREFIX.len()..];
- if line.starts_with(MULTILINE_CONT) {
- line = &line[MULTILINE_CONT.len()..];
+ if let Some(new_line) = line.strip_str_prefix(MULTILINE_CONT) {
+ line = new_line;
asterisk_indented = true;
} else {
asterisk_indented = false;
@@ -38,8 +38,8 @@ pub fn strip_comment_markers(comment: &str) -> String {
asterisk_indented = false;
}
} else {
- if line_clean.starts_with(MULTILINE_PREFIX) {
- line = line_clean[MULTILINE_PREFIX.len()..]
+ if let Some(line_clean) = line_clean.strip_str_prefix(MULTILINE_PREFIX) {
+ line = line_clean
.trim_start_matches(DETAIL)
.trim_start_matches(MULTILINE_CONT)
.trim_start();
@@ -47,13 +47,13 @@ pub fn strip_comment_markers(comment: &str) -> String {
}
if singleline_delimited && line_clean.starts_with(SINGLELINE) {
line = &line_clean[SINGLELINE.len()..];
- if line.starts_with(SINGLELINE_DETAIL) {
- line = &line[SINGLELINE_DETAIL.len()..];
- } else if line.starts_with(DETAIL) {
- line = &line[DETAIL.len()..];
+ if let Some(new_line) = line.strip_str_prefix(SINGLELINE_DETAIL) {
+ line = new_line;
+ } else if let Some(new_line) = line.strip_str_prefix(DETAIL) {
+ line = new_line;
}
- if line.starts_with(SINGLELINE_SIDE) {
- line = &line[SINGLELINE_SIDE.len()..];
+ if let Some(new_line) = line.strip_str_prefix(SINGLELINE_SIDE) {
+ line = new_line;
}
} else if asterisk_indented && i == 1 && !line_clean.starts_with(MULTILINE_CONT) {
asterisk_indented = false;
@@ -89,8 +89,8 @@ pub fn strip_comment_markers(comment: &str) -> String {
for line in &mut lines {
if !singleline_delimited && asterisk_indented {
let line_trimmed = line.trim_start();
- if line_trimmed.starts_with(MULTILINE_CONT) {
- *line = &line_trimmed[MULTILINE_CONT.len()..];
+ if let Some(line_trimmed) = line_trimmed.strip_str_prefix(MULTILINE_CONT) {
+ *line = line_trimmed;
} else {
let trim_start = line.trim_start_idx().min(2);
*line = &line[trim_start..];

MODIFY
binding-generator/src/func.rs
binding-generator/src/func.rs
@@ -109,9 +109,8 @@ impl<'tu, 'g> Func<'tu, 'g> {
const OPERATOR: &str = "operator";
match self.entity.get_kind() {
EntityKind::FunctionDecl => {
- let name = self.entity.cpp_localname();
- if name.starts_with(OPERATOR) {
- Kind::FunctionOperator(name[OPERATOR.len()..].into())
+ if let Some(operator) = self.entity.cpp_localname().strip_str_prefix(OPERATOR) {
+ Kind::FunctionOperator(operator.into())
} else {
Kind::Function
}
@@ -130,9 +129,8 @@ impl<'tu, 'g> Func<'tu, 'g> {
if self.entity.is_static_method() {
Kind::StaticMethod(class)
} else {
- let name = self.entity.cpp_localname();
- if name.starts_with(OPERATOR) {
- Kind::InstanceOperator(class, name[OPERATOR.len()..].into())
+ if let Some(operator) = self.entity.cpp_localname().strip_str_prefix(OPERATOR) {
+ Kind::InstanceOperator(class, operator.into())
} else {
Kind::InstanceMethod(class)
}

MODIFY
binding-generator/src/string_ext.rs
binding-generator/src/string_ext.rs
@@ -328,6 +328,7 @@ pub trait StrExt {
fn compile_interpolation(&self) -> CompiledInterpolation;
fn trim_start_idx(&self) -> usize;
fn trim_end_idx(&self) -> usize;
+ fn strip_str_prefix(&self, prefix: &str) -> Option<&str>;
}
impl StrExt for str {
@@ -422,4 +423,12 @@ impl StrExt for str {
.rfind(|(_, c)| !c.is_whitespace())
.map_or(0, |(i, _)| i + 1)
}
+
+ fn strip_str_prefix(&self, prefix: &str) -> Option<&str> {
+ if self.starts_with(prefix) {
+ Some(unsafe { self.get_unchecked(prefix.len()..) })
+ } else {
+ None
+ }
+ }
}

Add strip_str_prefix function