Bug 1330824 - Add truncate for nsAString and nsACString. r?mystor
MozReview-Commit-ID: 2ND5ra3buxI
--- a/servo/components/style/gecko_bindings/nsstring_vendor/src/lib.rs
+++ b/servo/components/style/gecko_bindings/nsstring_vendor/src/lib.rs
@@ -569,16 +569,22 @@ impl nsACString {
unsafe {
Gecko_AppendUTF16toCString(self as *mut _, other as *const _);
}
}
pub unsafe fn as_str_unchecked(&self) -> &str {
str::from_utf8_unchecked(self)
}
+
+ pub fn truncate(&mut self) {
+ unsafe {
+ Gecko_TruncateCString(self);
+ }
+ }
}
impl<'a> From<&'a str> for nsCString<'a> {
fn from(s: &'a str) -> nsCString<'a> {
s.as_bytes().into()
}
}
@@ -669,16 +675,22 @@ impl nsAString {
}
}
pub fn append_utf8(&mut self, other: &nsACString) {
unsafe {
Gecko_AppendUTF8toString(self as *mut _, other as *const _);
}
}
+
+ pub fn truncate(&mut self) {
+ unsafe {
+ Gecko_TruncateString(self);
+ }
+ }
}
// NOTE: The From impl for a string slice for nsString produces a <'static>
// lifetime, as it allocates.
impl<'a> From<&'a str> for nsString<'static> {
fn from(s: &'a str) -> nsString<'static> {
s.encode_utf16().collect::<Vec<u16>>().into()
}
@@ -722,17 +734,19 @@ macro_rules! ns_auto_string {
// NOTE: These bindings currently only expose infallible operations. Perhaps
// consider allowing for fallible methods?
extern "C" {
// Gecko implementation in nsSubstring.cpp
fn Gecko_FinalizeCString(this: *mut nsACString);
fn Gecko_AssignCString(this: *mut nsACString, other: *const nsACString);
fn Gecko_AppendCString(this: *mut nsACString, other: *const nsACString);
+ fn Gecko_TruncateCString(this: *mut nsACString);
fn Gecko_FinalizeString(this: *mut nsAString);
fn Gecko_AssignString(this: *mut nsAString, other: *const nsAString);
fn Gecko_AppendString(this: *mut nsAString, other: *const nsAString);
+ fn Gecko_TruncateString(this: *mut nsAString);
// Gecko implementation in nsReadableUtils.cpp
fn Gecko_AppendUTF16toCString(this: *mut nsACString, other: *const nsAString);
fn Gecko_AppendUTF8toString(this: *mut nsAString, other: *const nsACString);
}
--- a/xpcom/rust/nsstring/src/lib.rs
+++ b/xpcom/rust/nsstring/src/lib.rs
@@ -596,16 +596,22 @@ impl nsACString {
unsafe {
Gecko_AppendUTF16toCString(self, &*s);
}
}
pub unsafe fn as_str_unchecked(&self) -> &str {
str::from_utf8_unchecked(self)
}
+
+ pub fn truncate(&mut self) {
+ unsafe {
+ Gecko_TruncateCString(self);
+ }
+ }
}
impl<'a> From<&'a str> for nsCString<'a> {
fn from(s: &'a str) -> nsCString<'a> {
s.as_bytes().into()
}
}
@@ -699,16 +705,22 @@ impl nsAString {
}
pub fn append_utf8<T: AsRef<[u8]> + ?Sized>(&mut self, other: &T) {
let s = nsCString::from(other.as_ref());
unsafe {
Gecko_AppendUTF8toString(self, &*s);
}
}
+
+ pub fn truncate(&mut self) {
+ unsafe {
+ Gecko_TruncateString(self);
+ }
+ }
}
// NOTE: The From impl for a string slice for nsString produces a <'static>
// lifetime, as it allocates.
impl<'a> From<&'a str> for nsString<'static> {
fn from(s: &'a str) -> nsString<'static> {
s.encode_utf16().collect::<Vec<u16>>().into()
}
@@ -752,20 +764,22 @@ macro_rules! ns_auto_string {
// NOTE: These bindings currently only expose infallible operations. Perhaps
// consider allowing for fallible methods?
extern "C" {
// Gecko implementation in nsSubstring.cpp
fn Gecko_FinalizeCString(this: *mut nsACString);
fn Gecko_AssignCString(this: *mut nsACString, other: *const nsACString);
fn Gecko_AppendCString(this: *mut nsACString, other: *const nsACString);
+ fn Gecko_TruncateCString(this: *mut nsACString);
fn Gecko_FinalizeString(this: *mut nsAString);
fn Gecko_AssignString(this: *mut nsAString, other: *const nsAString);
fn Gecko_AppendString(this: *mut nsAString, other: *const nsAString);
+ fn Gecko_TruncateString(this: *mut nsAString);
// Gecko implementation in nsReadableUtils.cpp
fn Gecko_AppendUTF16toCString(this: *mut nsACString, other: *const nsAString);
fn Gecko_AppendUTF8toString(this: *mut nsAString, other: *const nsACString);
}
//////////////////////////////////////
// Repr Validation Helper Functions //
--- a/xpcom/string/nsSubstring.cpp
+++ b/xpcom/string/nsSubstring.cpp
@@ -363,24 +363,34 @@ void Gecko_AssignCString(nsACString* aTh
aThis->Assign(*aOther);
}
void Gecko_AppendCString(nsACString* aThis, const nsACString* aOther)
{
aThis->Append(*aOther);
}
+void Gecko_TruncateCString(nsACString* aThis)
+{
+ aThis->Truncate();
+}
+
void Gecko_FinalizeString(nsAString* aThis)
{
aThis->~nsAString();
}
void Gecko_AssignString(nsAString* aThis, const nsAString* aOther)
{
aThis->Assign(*aOther);
}
void Gecko_AppendString(nsAString* aThis, const nsAString* aOther)
{
aThis->Append(*aOther);
}
+void Gecko_TruncateString(nsAString* aThis)
+{
+ aThis->Truncate();
+}
+
} // extern "C"