@@ -68,6 +68,7 @@ use iter::{Iterator, range};
68
68
use libc;
69
69
use kinds:: marker;
70
70
use ops:: Drop ;
71
+ use cmp:: Eq ;
71
72
use clone:: Clone ;
72
73
use option:: { Option , Some , None } ;
73
74
use ptr:: RawPtr ;
@@ -109,13 +110,28 @@ impl Clone for CString {
109
110
if self . buf . is_null ( ) {
110
111
CString { buf : self . buf , owns_buffer_ : self . owns_buffer_ }
111
112
} else {
112
- let buf = unsafe { malloc_raw ( self . len ( ) ) } as * mut libc:: c_char ;
113
- unsafe { ptr:: copy_nonoverlapping_memory ( buf, self . buf , self . len ( ) ) ; }
113
+ let len = self . len ( ) + 1 ;
114
+ let buf = unsafe { malloc_raw ( len) } as * mut libc:: c_char ;
115
+ unsafe { ptr:: copy_nonoverlapping_memory ( buf, self . buf , len) ; }
114
116
CString { buf : buf as * libc:: c_char , owns_buffer_ : true }
115
117
}
116
118
}
117
119
}
118
120
121
+ impl Eq for CString {
122
+ fn eq ( & self , other : & CString ) -> bool {
123
+ if self . buf as uint == other. buf as uint {
124
+ true
125
+ } else if self . buf . is_null ( ) || other. buf . is_null ( ) {
126
+ false
127
+ } else {
128
+ unsafe {
129
+ libc:: strcmp ( self . buf , other. buf ) == 0
130
+ }
131
+ }
132
+ }
133
+ }
134
+
119
135
impl CString {
120
136
/// Create a C String from a pointer.
121
137
pub unsafe fn new ( buf : * libc:: c_char , owns_buffer : bool ) -> CString {
@@ -615,8 +631,9 @@ mod tests {
615
631
616
632
#[ test]
617
633
fn test_clone ( ) {
618
- let c_str = "hello" . to_c_str ( ) ;
619
- assert ! ( c_str == c_str. clone( ) ) ;
634
+ let a = "hello" . to_c_str ( ) ;
635
+ let b = a. clone ( ) ;
636
+ assert ! ( a == b) ;
620
637
}
621
638
622
639
#[ test]
@@ -642,6 +659,13 @@ mod tests {
642
659
// force a copy, reading the memory
643
660
c_. as_bytes ( ) . to_owned ( ) ;
644
661
}
662
+
663
+ #[ test]
664
+ fn test_clone_eq_null ( ) {
665
+ let x = unsafe { CString :: new ( ptr:: null ( ) , false ) } ;
666
+ let y = x. clone ( ) ;
667
+ assert ! ( x == y) ;
668
+ }
645
669
}
646
670
647
671
#[ cfg( test) ]
0 commit comments