Skip to content

Commit 1a610f9

Browse files
authored
Return string reference from std::string::append (#642)
Fixes #612
1 parent c1ba4f0 commit 1a610f9

5 files changed

Lines changed: 163 additions & 51 deletions

File tree

‎rules/string/tgt_refcount.rs‎

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,16 @@ fn f18(a0: Vec<i8>, a1: Ptr<i8>) -> bool {
119119
a1.with_c_str(|__s| a0[..a0.len().saturating_sub(1)] == *__s)
120120
}
121121

122-
// TODO: This should return a0
123-
fn f21(a0: &mut Vec<i8>, a1: usize, a2: i8) -> Vec<i8> {
124-
a0.pop();
125-
let __len = a0.len() + (a1) as usize;
126-
a0.resize(__len, a2);
127-
a0.push(0);
128-
a0.clone()
122+
fn f21(a0: Ptr<Vec<i8>>, a1: usize, a2: i8) -> Ptr<Vec<i8>> {
123+
let __count = a1 as usize;
124+
let __ch = a2;
125+
a0.with_mut(|__v: &mut Vec<i8>| {
126+
__v.pop();
127+
let __len = __v.len() + __count;
128+
__v.resize(__len, __ch);
129+
__v.push(0);
130+
});
131+
a0
129132
}
130133

131134
fn f26(a0: Ptr<Vec<i8>>, a1: usize) -> Ptr<i8> {

‎rules/string/tgt_unsafe.rs‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,12 @@ unsafe fn f18(a0: Vec<libc::c_char>, a1: *const libc::c_char) -> bool {
145145
unsafe fn f19(a0: Vec<libc::c_char>) -> usize {
146146
(a0.len() - 1)
147147
}
148-
unsafe fn f21(a0: &mut Vec<libc::c_char>, a1: usize, a2: libc::c_char) {
149-
a0.splice(
150-
a0.len() - 1..a0.len() - 1,
151-
::std::vec::from_elem(a2, a1),
148+
unsafe fn f21(a0: *mut Vec<libc::c_char>, a1: usize, a2: libc::c_char) -> *mut Vec<libc::c_char> {
149+
(*a0).splice(
150+
(*a0).len() - 1..(*a0).len() - 1,
151+
::std::vec::from_elem(a2, a1 as usize),
152152
);
153+
a0
153154
}
154155

155156
unsafe fn f22(a0: Vec<libc::c_char>) -> bool {

‎tests/unit/out/refcount/string_repeat_append.rs‎

Lines changed: 79 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ use std::os::fd::AsFd;
88
use std::rc::{Rc, Weak};
99
pub fn append_to_reference_0(s: Ptr<Vec<i8>>) {
1010
{
11-
s.with_mut(|__v: &mut Vec<i8>| __v.pop());
12-
let __len = (*s.upgrade().deref()).len() + (1_usize) as usize;
13-
s.with_mut(|__v: &mut Vec<i8>| __v.resize(__len, ('r' as i8)));
14-
s.with_mut(|__v: &mut Vec<i8>| __v.push(0));
15-
(*s.upgrade().deref()).clone()
11+
let __count = 1_usize as usize;
12+
let __ch = ('r' as i8);
13+
s.with_mut(|__v: &mut Vec<i8>| {
14+
__v.pop();
15+
let __len = __v.len() + __count;
16+
__v.resize(__len, __ch);
17+
__v.push(0);
18+
});
19+
(s).clone()
1620
};
1721
}
1822
pub fn main() {
@@ -92,11 +96,15 @@ fn main_0() -> i32 {
9296
(*s.borrow_mut()).shrink_to_fit();
9397
}
9498
{
95-
(*s.borrow_mut()).pop();
96-
let __len = (*s.borrow()).len() + (count) as usize;
97-
(*s.borrow_mut()).resize(__len, character);
98-
(*s.borrow_mut()).push(0);
99-
(*s.borrow()).clone()
99+
let __count = count as usize;
100+
let __ch = character;
101+
s.as_pointer().with_mut(|__v: &mut Vec<i8>| {
102+
__v.pop();
103+
let __len = __v.len() + __count;
104+
__v.resize(__len, __ch);
105+
__v.push(0);
106+
});
107+
s.as_pointer()
100108
};
101109
assert!(({ (((*s.borrow()).len() - 1) == (length).wrapping_add(count)) }));
102110
let mut data: Ptr<i8> = s.decay();
@@ -128,21 +136,28 @@ fn main_0() -> i32 {
128136
}
129137
}
130138
}
131-
let mut unboxed: Vec<i8> = {
139+
let unboxed: Value<Vec<i8>> = Rc::new(RefCell::new({
132140
let mut __bytes = Ptr::<i8>::from_string_literal(b"a").to_c_bytes();
133141
__bytes.push(0);
134142
__bytes
135-
};
143+
}));
136144
{
137-
unboxed.pop();
138-
let __len = unboxed.len() + (2_usize) as usize;
139-
unboxed.resize(__len, ('b' as i8));
140-
unboxed.push(0);
141-
unboxed.clone()
145+
let __count = 2_usize as usize;
146+
let __ch = ('b' as i8);
147+
unboxed.as_pointer().with_mut(|__v: &mut Vec<i8>| {
148+
__v.pop();
149+
let __len = __v.len() + __count;
150+
__v.resize(__len, __ch);
151+
__v.push(0);
152+
});
153+
unboxed.as_pointer()
142154
};
143155
assert!(
144-
Ptr::<i8>::from_string_literal(b"abb")
145-
.with_c_str(|__s| unboxed[..unboxed.len().saturating_sub(1)] == *__s)
156+
({
157+
Ptr::<i8>::from_string_literal(b"abb").with_c_str(|__s| {
158+
(*unboxed.borrow())[..(*unboxed.borrow()).len().saturating_sub(1)] == *__s
159+
})
160+
})
146161
);
147162
let indirect: Value<Vec<i8>> = Rc::new(RefCell::new({
148163
let mut __bytes = Ptr::<i8>::from_string_literal(b"c").to_c_bytes();
@@ -159,11 +174,15 @@ fn main_0() -> i32 {
159174
);
160175
let mut pointer: Ptr<Vec<i8>> = (indirect.as_pointer());
161176
{
162-
pointer.with_mut(|__v: &mut Vec<i8>| __v.pop());
163-
let __len = (*pointer.upgrade().deref()).len() + (0_usize) as usize;
164-
pointer.with_mut(|__v: &mut Vec<i8>| __v.resize(__len, ('\0' as i8)));
165-
pointer.with_mut(|__v: &mut Vec<i8>| __v.push(0));
166-
(*pointer.upgrade().deref()).clone()
177+
let __count = 0_usize as usize;
178+
let __ch = ('\0' as i8);
179+
pointer.with_mut(|__v: &mut Vec<i8>| {
180+
__v.pop();
181+
let __len = __v.len() + __count;
182+
__v.resize(__len, __ch);
183+
__v.push(0);
184+
});
185+
(pointer).clone()
167186
};
168187
assert!(
169188
({
@@ -172,6 +191,42 @@ fn main_0() -> i32 {
172191
})
173192
})
174193
);
194+
let chained: Value<Vec<i8>> = Rc::new(RefCell::new({
195+
let mut __bytes = Ptr::<i8>::from_string_literal(b"a").to_c_bytes();
196+
__bytes.push(0);
197+
__bytes
198+
}));
199+
match ({
200+
let __count = 3_usize as usize;
201+
let __ch = ('x' as i8);
202+
chained.as_pointer().with_mut(|__v: &mut Vec<i8>| {
203+
__v.pop();
204+
let __len = __v.len() + __count;
205+
__v.resize(__len, __ch);
206+
__v.push(0);
207+
});
208+
chained.as_pointer()
209+
},)
210+
{
211+
(__tmp_0,) => {
212+
let __count = 2_usize as usize;
213+
let __ch = ('y' as i8);
214+
__tmp_0.with_mut(|__v: &mut Vec<i8>| {
215+
__v.pop();
216+
let __len = __v.len() + __count;
217+
__v.resize(__len, __ch);
218+
__v.push(0);
219+
});
220+
(__tmp_0).clone()
221+
}
222+
};
223+
assert!(
224+
({
225+
Ptr::<i8>::from_string_literal(b"axxxyy").with_c_str(|__s| {
226+
(*chained.borrow())[..(*chained.borrow()).len().saturating_sub(1)] == *__s
227+
})
228+
})
229+
);
175230
println!("checked {} appends", cases);
176231
return 0;
177232
}

‎tests/unit/out/unsafe/string_repeat_append.rs‎

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ use std::io::{Read, Seek, Write};
77
use std::os::fd::{AsFd, FromRawFd, IntoRawFd};
88
use std::rc::Rc;
99
pub unsafe fn append_to_reference_0(s: *mut Vec<libc::c_char>) {
10-
(*s).splice(
11-
(*s).len() - 1..(*s).len() - 1,
12-
::std::vec::from_elem(('r' as libc::c_char), 1_usize),
13-
);
10+
{
11+
(*(s as *mut Vec<libc::c_char>)).splice(
12+
(*(s as *mut Vec<libc::c_char>)).len() - 1..(*(s as *mut Vec<libc::c_char>)).len() - 1,
13+
::std::vec::from_elem(('r' as libc::c_char), 1_usize as usize),
14+
);
15+
(s as *mut Vec<libc::c_char>)
16+
};
1417
}
1518
pub fn main() {
1619
unsafe {
@@ -88,10 +91,14 @@ unsafe fn main_0() -> i32 {
8891
if (shrink != 0) {
8992
s.shrink_to_fit();
9093
}
91-
s.splice(
92-
s.len() - 1..s.len() - 1,
93-
::std::vec::from_elem(character, count),
94-
);
94+
{
95+
(*(&mut s as *mut Vec<libc::c_char>)).splice(
96+
(*(&mut s as *mut Vec<libc::c_char>)).len() - 1
97+
..(*(&mut s as *mut Vec<libc::c_char>)).len() - 1,
98+
::std::vec::from_elem(character, count as usize),
99+
);
100+
(&mut s as *mut Vec<libc::c_char>)
101+
};
95102
assert!(((s.len() - 1) == ((length).wrapping_add(count))));
96103
let mut data: *const libc::c_char = (s.as_mut_ptr()).cast_const();
97104
{
@@ -124,10 +131,14 @@ unsafe fn main_0() -> i32 {
124131
(0..).take_while(|&i| *c"a".as_ptr().add(i) != 0).count() + 1,
125132
)
126133
.to_vec();
127-
unboxed.splice(
128-
unboxed.len() - 1..unboxed.len() - 1,
129-
::std::vec::from_elem(('b' as libc::c_char), 2_usize),
130-
);
134+
{
135+
(*(&mut unboxed as *mut Vec<libc::c_char>)).splice(
136+
(*(&mut unboxed as *mut Vec<libc::c_char>)).len() - 1
137+
..(*(&mut unboxed as *mut Vec<libc::c_char>)).len() - 1,
138+
::std::vec::from_elem(('b' as libc::c_char), 2_usize as usize),
139+
);
140+
(&mut unboxed as *mut Vec<libc::c_char>)
141+
};
131142
assert!(
132143
unboxed == {
133144
std::slice::from_raw_parts(
@@ -153,10 +164,14 @@ unsafe fn main_0() -> i32 {
153164
}
154165
);
155166
let mut pointer: *mut Vec<libc::c_char> = (&mut indirect as *mut Vec<libc::c_char>);
156-
(*pointer).splice(
157-
(*pointer).len() - 1..(*pointer).len() - 1,
158-
::std::vec::from_elem(('\0' as libc::c_char), 0_usize),
159-
);
167+
{
168+
(*(pointer as *mut Vec<libc::c_char>)).splice(
169+
(*(pointer as *mut Vec<libc::c_char>)).len() - 1
170+
..(*(pointer as *mut Vec<libc::c_char>)).len() - 1,
171+
::std::vec::from_elem(('\0' as libc::c_char), 0_usize as usize),
172+
);
173+
(pointer as *mut Vec<libc::c_char>)
174+
};
160175
assert!(
161176
indirect == {
162177
std::slice::from_raw_parts(
@@ -166,6 +181,41 @@ unsafe fn main_0() -> i32 {
166181
.to_vec()
167182
}
168183
);
184+
let mut chained: Vec<libc::c_char> = std::slice::from_raw_parts(
185+
c"a".as_ptr(),
186+
(0..).take_while(|&i| *c"a".as_ptr().add(i) != 0).count() + 1,
187+
)
188+
.to_vec();
189+
match (({
190+
(*(&mut chained as *mut Vec<libc::c_char>)).splice(
191+
(*(&mut chained as *mut Vec<libc::c_char>)).len() - 1
192+
..(*(&mut chained as *mut Vec<libc::c_char>)).len() - 1,
193+
::std::vec::from_elem(('x' as libc::c_char), 3_usize as usize),
194+
);
195+
(&mut chained as *mut Vec<libc::c_char>)
196+
} as *mut Vec<libc::c_char>),)
197+
{
198+
(__tmp_0,) => {
199+
(*(__tmp_0 as *mut Vec<libc::c_char>)).splice(
200+
(*(__tmp_0 as *mut Vec<libc::c_char>)).len() - 1
201+
..(*(__tmp_0 as *mut Vec<libc::c_char>)).len() - 1,
202+
::std::vec::from_elem(('y' as libc::c_char), 2_usize as usize),
203+
);
204+
(__tmp_0 as *mut Vec<libc::c_char>)
205+
}
206+
};
207+
assert!(
208+
chained == {
209+
std::slice::from_raw_parts(
210+
c"axxxyy".as_ptr(),
211+
(0..)
212+
.take_while(|&i| *c"axxxyy".as_ptr().add(i) != 0)
213+
.count()
214+
+ 1,
215+
)
216+
.to_vec()
217+
}
218+
);
169219
(unsafe {
170220
libc::printf(
171221
c"checked %zu appends\n".as_ptr() as *const libc::c_char,

‎tests/unit/string_repeat_append.cpp‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ int main() {
4242
std::string *pointer = &indirect;
4343
pointer->append(std::size_t(0), '\0');
4444
assert(indirect == "cr");
45+
std::string chained = "a";
46+
chained.append(3, 'x').append(2, 'y');
47+
assert(chained == "axxxyy");
4548
printf("checked %zu appends\n", cases);
4649
return 0;
4750
}

0 commit comments

Comments
 (0)