Bug 1330824 - Add truncate for nsAString and nsACString. r=mystor
authorHiroyuki Ikezoe <hikezoe@mozilla.com>
Sat, 14 Jan 2017 15:02:14 +0900
changeset 374455 f9408aa59a926dede0e08d4d491bcabd75d533d6
parent 374454 6b716be52be01188cd7c5f82538c55f83d9dad27
child 374456 9735ab23b94303279482161008ae06b6528fa32b
push id6996
push userjlorenzo@mozilla.com
push dateMon, 06 Mar 2017 20:48:21 +0000
treeherdermozilla-beta@d89512dab048 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmystor
bugs1330824
milestone53.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1330824 - Add truncate for nsAString and nsACString. r=mystor MozReview-Commit-ID: 2ND5ra3buxI
xpcom/rust/nsstring/src/lib.rs
xpcom/string/nsSubstring.cpp
--- 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"