Skip to content

Commit 5865a75

Browse files
committed
Remove trailing null from strings
1 parent 17e0089 commit 5865a75

File tree

12 files changed

+544
-43
lines changed

12 files changed

+544
-43
lines changed

src/libextra/terminfo/parm.rs

Lines changed: 98 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ impl FormatOp {
476476
}
477477
}
478478

479+
#[cfg(stage0)]
479480
priv fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
480481
let mut s = match val {
481482
Number(d) => {
@@ -545,8 +546,103 @@ priv fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
545546
String(s) => {
546547
match op {
547548
FormatString => {
548-
let mut s = s.to_bytes_with_null();
549-
s.pop(); // remove the null
549+
let mut s = s.as_bytes().to_owned();
550+
if flags.precision > 0 && flags.precision < s.len() {
551+
s.truncate(flags.precision);
552+
}
553+
s
554+
}
555+
_ => {
556+
return Err(fmt!("non-string on stack with %%%c", op.to_char()))
557+
}
558+
}
559+
}
560+
};
561+
if flags.width > s.len() {
562+
let n = flags.width - s.len();
563+
if flags.left {
564+
s.grow(n, &(' ' as u8));
565+
} else {
566+
let mut s_ = vec::with_capacity(flags.width);
567+
s_.grow(n, &(' ' as u8));
568+
s_.push_all_move(s);
569+
s = s_;
570+
}
571+
}
572+
Ok(s)
573+
}
574+
575+
#[cfg(not(stage0))]
576+
priv fn format(val: Param, op: FormatOp, flags: Flags) -> Result<~[u8],~str> {
577+
let mut s = match val {
578+
Number(d) => {
579+
match op {
580+
FormatString => {
581+
return Err(~"non-number on stack with %s")
582+
}
583+
_ => {
584+
let radix = match op {
585+
FormatDigit => 10,
586+
FormatOctal => 8,
587+
FormatHex|FormatHEX => 16,
588+
FormatString => util::unreachable()
589+
};
590+
let mut s = ~[];
591+
match op {
592+
FormatDigit => {
593+
let sign = if flags.sign { SignAll } else { SignNeg };
594+
do int_to_str_bytes_common(d, radix, sign) |c| {
595+
s.push(c);
596+
}
597+
}
598+
_ => {
599+
do int_to_str_bytes_common(d as uint, radix, SignNone) |c| {
600+
s.push(c);
601+
}
602+
}
603+
};
604+
if flags.precision > s.len() {
605+
let mut s_ = vec::with_capacity(flags.precision);
606+
let n = flags.precision - s.len();
607+
s_.grow(n, &('0' as u8));
608+
s_.push_all_move(s);
609+
s = s_;
610+
}
611+
assert!(!s.is_empty(), "string conversion produced empty result");
612+
match op {
613+
FormatDigit => {
614+
if flags.space && !(s[0] == '-' as u8 || s[0] == '+' as u8) {
615+
s.unshift(' ' as u8);
616+
}
617+
}
618+
FormatOctal => {
619+
if flags.alternate && s[0] != '0' as u8 {
620+
s.unshift('0' as u8);
621+
}
622+
}
623+
FormatHex => {
624+
if flags.alternate {
625+
let s_ = util::replace(&mut s, ~['0' as u8, 'x' as u8]);
626+
s.push_all_move(s_);
627+
}
628+
}
629+
FormatHEX => {
630+
s = s.into_ascii().to_upper().into_bytes();
631+
if flags.alternate {
632+
let s_ = util::replace(&mut s, ~['0' as u8, 'X' as u8]);
633+
s.push_all_move(s_);
634+
}
635+
}
636+
FormatString => util::unreachable()
637+
}
638+
s
639+
}
640+
}
641+
}
642+
String(s) => {
643+
match op {
644+
FormatString => {
645+
let mut s = s.as_bytes().to_owned();
550646
if flags.precision > 0 && flags.precision < s.len() {
551647
s.truncate(flags.precision);
552648
}

src/librustc/middle/trans/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ pub fn C_estr_slice(cx: &mut CrateContext, s: @str) -> ValueRef {
780780
unsafe {
781781
let len = s.len();
782782
let cs = llvm::LLVMConstPointerCast(C_cstr(cx, s), Type::i8p().to_ref());
783-
C_struct([cs, C_uint(cx, len + 1u /* +1 for null */)])
783+
C_struct([cs, C_uint(cx, len)])
784784
}
785785
}
786786

src/librustc/middle/trans/expr.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,6 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: @ast::expr) -> DatumBlock {
870870
871871
let _icx = push_ctxt("trans_index");
872872
let ccx = bcx.ccx();
873-
let base_ty = expr_ty(bcx, base);
874873
let mut bcx = bcx;
875874

876875
let base_datum = unpack_datum!(bcx, trans_to_datum(bcx, base));
@@ -900,12 +899,6 @@ fn trans_lvalue_unadjusted(bcx: @mut Block, expr: @ast::expr) -> DatumBlock {
900899
let (bcx, base, len) =
901900
base_datum.get_vec_base_and_len(bcx, index_expr.span,
902901
index_expr.id, 0);
903-
let mut len = len;
904-
905-
if ty::type_is_str(base_ty) {
906-
// acccount for null terminator in the case of string
907-
len = Sub(bcx, len, C_uint(bcx.ccx(), 1u));
908-
}
909902

910903
debug!("trans_index: base %s", bcx.val_to_str(base));
911904
debug!("trans_index: len %s", bcx.val_to_str(len));

src/librustc/middle/trans/reflect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ impl Reflector {
5858
let str_vstore = ty::vstore_slice(ty::re_static);
5959
let str_ty = ty::mk_estr(bcx.tcx(), str_vstore);
6060
let scratch = scratch_datum(bcx, str_ty, "", false);
61-
let len = C_uint(bcx.ccx(), s.len() + 1);
61+
let len = C_uint(bcx.ccx(), s.len());
6262
let c_str = PointerCast(bcx, C_cstr(bcx.ccx(), s), Type::i8p());
6363
Store(bcx, c_str, GEPi(bcx, scratch.val, [ 0, 0 ]));
6464
Store(bcx, len, GEPi(bcx, scratch.val, [ 0, 1 ]));

src/librustc/middle/trans/tvec.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ pub fn trans_lit_str(bcx: @mut Block,
265265
Ignore => bcx,
266266
SaveIn(lldest) => {
267267
unsafe {
268-
let bytes = str_lit.len() + 1; // count null-terminator too
268+
let bytes = str_lit.len(); // count null-terminator too
269269
let llbytes = C_uint(bcx.ccx(), bytes);
270270
let llcstr = C_cstr(bcx.ccx(), str_lit);
271271
let llcstr = llvm::LLVMConstPointerCast(llcstr, Type::i8p().to_ref());
@@ -363,7 +363,7 @@ pub fn write_content(bcx: @mut Block,
363363
return bcx;
364364
}
365365
SaveIn(lldest) => {
366-
let bytes = s.len() + 1; // copy null-terminator too
366+
let bytes = s.len();
367367
let llbytes = C_uint(bcx.ccx(), bytes);
368368
let llcstr = C_cstr(bcx.ccx(), s);
369369
base::call_memcpy(bcx, lldest, llcstr, llbytes, 1);
@@ -491,7 +491,7 @@ pub fn elements_required(bcx: @mut Block, content_expr: &ast::expr) -> uint {
491491
492492
match content_expr.node {
493493
ast::expr_lit(@codemap::spanned { node: ast::lit_str(s), _ }) => {
494-
s.len() + 1
494+
s.len()
495495
},
496496
ast::expr_vec(ref es, _) => es.len(),
497497
ast::expr_repeat(_, count_expr, _) => {
@@ -524,7 +524,6 @@ pub fn get_base_and_len(bcx: @mut Block,
524524
match vstore {
525525
ty::vstore_fixed(n) => {
526526
let base = GEPi(bcx, llval, [0u, 0u]);
527-
let n = if ty::type_is_str(vec_ty) { n + 1u } else { n };
528527
let len = Mul(bcx, C_uint(ccx, n), vt.llunit_size);
529528
(base, len)
530529
}

src/libstd/cast.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,20 @@ mod tests {
165165
}
166166
}
167167
168+
#[cfg(stage0)]
168169
#[test]
169170
fn test_transmute2() {
170171
unsafe {
171172
assert_eq!(~[76u8, 0u8], transmute(~"L"));
172173
}
173174
}
175+
176+
#[cfg(not(stage0))]
177+
#[test]
178+
fn test_transmute2() {
179+
unsafe {
180+
assert_eq!(~[76u8], transmute(~"L"));
181+
}
182+
}
183+
174184
}

src/libstd/io.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,6 +1707,7 @@ pub fn with_bytes_writer(f: &fn(@Writer)) -> ~[u8] {
17071707
(*bytes).clone()
17081708
}
17091709

1710+
#[cfg(stage0)]
17101711
pub fn with_str_writer(f: &fn(@Writer)) -> ~str {
17111712
let mut v = with_bytes_writer(f);
17121713

@@ -1719,6 +1720,11 @@ pub fn with_str_writer(f: &fn(@Writer)) -> ~str {
17191720
}
17201721
}
17211722

1723+
#[cfg(not(stage0))]
1724+
pub fn with_str_writer(f: &fn(@Writer)) -> ~str {
1725+
str::from_bytes(with_bytes_writer(f))
1726+
}
1727+
17221728
// Utility functions
17231729
pub fn seek_in_buf(offset: int, pos: uint, len: uint, whence: SeekStyle) ->
17241730
uint {

src/libstd/run.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -758,7 +758,8 @@ fn with_envp<T>(env: Option<&[(~str, ~str)]>, cb: &fn(*mut c_void) -> T) -> T {
758758

759759
foreach pair in env.iter() {
760760
let kv = fmt!("%s=%s", pair.first(), pair.second());
761-
blk.push_all(kv.to_c_str().as_bytes());
761+
blk.push_all(kv.as_bytes());
762+
blk.push(0);
762763
}
763764

764765
blk.push(0);

0 commit comments

Comments
 (0)