Merge pull request #2261 from topecongiro/replace-string-buffer

Replace `StringBuffer` with `String`
This commit is contained in:
Nick Cameron 2017-12-11 15:39:55 +13:00 committed by GitHub
commit aec8cfa749
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 42 deletions

10
Cargo.lock generated
View file

@ -186,7 +186,6 @@ dependencies = [
"serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
"serde_json 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)",
"strings 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)",
"term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)",
"toml 0.4.5 (registry+https://github.com/rust-lang/crates.io-index)",
"unicode-segmentation 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@ -242,14 +241,6 @@ dependencies = [
"serde 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "strings"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"log 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "syn"
version = "0.11.11"
@ -363,7 +354,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
"checksum serde_derive 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "652bc323d694dc925829725ec6c890156d8e70ae5202919869cb00fe2eff3788"
"checksum serde_derive_internals 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)" = "32f1926285523b2db55df263d2aa4eb69ddcfa7a7eade6430323637866b513ab"
"checksum serde_json 1.0.6 (registry+https://github.com/rust-lang/crates.io-index)" = "e4586746d1974a030c48919731ecffd0ed28d0c40749d0d18d43b3a7d6c9b20e"
"checksum strings 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "aa481ee1bc42fc3df8195f91f7cb43cf8f2b71b48bac40bf5381cfaf7e481f3c"
"checksum syn 0.11.11 (registry+https://github.com/rust-lang/crates.io-index)" = "d3b891b9015c88c576343b9b3e41c2c11a51c219ef067b264bd9c8aa9b441dad"
"checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6"
"checksum term 0.4.6 (registry+https://github.com/rust-lang/crates.io-index)" = "fa63644f74ce96fbeb9b794f66aff2a52d601cbd5e80f4b97123e3899f4570f1"

View file

@ -38,7 +38,6 @@ serde_json = "1.0"
unicode-segmentation = "1.0.0"
regex = "0.2"
term = "0.4"
strings = "0.1"
diff = "0.1"
log = "0.3"
env_logger = "0.4"

View file

@ -13,8 +13,6 @@
use std::fs::{self, File};
use std::io::{self, BufWriter, Read, Write};
use strings::string_buffer::StringBuffer;
use checkstyle::{output_checkstyle_file, output_footer, output_header};
use config::{Config, NewlineStyle, WriteMode};
use rustfmt_diff::{make_diff, print_diff, Mismatch};
@ -22,10 +20,10 @@ use rustfmt_diff::{make_diff, print_diff, Mismatch};
// A map of the files of a crate, with their new content
pub type FileMap = Vec<FileRecord>;
pub type FileRecord = (String, StringBuffer);
pub type FileRecord = (String, String);
// Append a newline to the end of each file.
pub fn append_newline(s: &mut StringBuffer) {
pub fn append_newline(s: &mut String) {
s.push_str("\n");
}
@ -47,11 +45,7 @@ where
}
// Prints all newlines either as `\n` or as `\r\n`.
pub fn write_system_newlines<T>(
writer: T,
text: &StringBuffer,
config: &Config,
) -> Result<(), io::Error>
pub fn write_system_newlines<T>(writer: T, text: &String, config: &Config) -> Result<(), io::Error>
where
T: Write,
{
@ -71,7 +65,7 @@ where
match style {
NewlineStyle::Unix => write!(writer, "{}", text),
NewlineStyle::Windows => {
for (c, _) in text.chars() {
for c in text.chars() {
match c {
'\n' => write!(writer, "\r\n")?,
'\r' => continue,
@ -85,7 +79,7 @@ where
}
pub fn write_file<T>(
text: &StringBuffer,
text: &String,
filename: &str,
out: &mut T,
config: &Config,
@ -94,7 +88,7 @@ where
T: Write,
{
fn source_and_formatted_text(
text: &StringBuffer,
text: &String,
filename: &str,
config: &Config,
) -> Result<(String, String), io::Error> {
@ -109,7 +103,7 @@ where
fn create_diff(
filename: &str,
text: &StringBuffer,
text: &String,
config: &Config,
) -> Result<Vec<Mismatch>, io::Error> {
let (ori, fmt) = source_and_formatted_text(text, filename, config)?;

View file

@ -21,7 +21,6 @@ extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate strings;
extern crate syntax;
extern crate term;
extern crate unicode_segmentation;
@ -35,7 +34,6 @@ use std::rc::Rc;
use errors::{DiagnosticBuilder, Handler};
use errors::emitter::{ColorConfig, EmitterWriter};
use strings::string_buffer::StringBuffer;
use syntax::ast;
use syntax::codemap::{CodeMap, FilePathMapping};
use syntax::parse::{self, ParseSess};
@ -300,7 +298,7 @@ fn format_ast<F>(
mut after_file: F,
) -> Result<(FileMap, bool), io::Error>
where
F: FnMut(&str, &mut StringBuffer, &[(usize, usize)]) -> Result<bool, io::Error>,
F: FnMut(&str, &mut String, &[(usize, usize)]) -> Result<bool, io::Error>,
{
let mut result = FileMap::new();
// diff mode: check if any files are differing
@ -369,7 +367,7 @@ fn is_skipped_line(line_number: usize, skipped_range: &[(usize, usize)]) -> bool
// FIXME(#209) warn on bad license
// FIXME(#20) other stuff for parity with make tidy
fn format_lines(
text: &mut StringBuffer,
text: &mut String,
name: &str,
skipped_range: &[(usize, usize)],
config: &Config,
@ -386,8 +384,10 @@ fn format_lines(
let mut prev_char: Option<char> = None;
let mut is_comment = false;
let mut line_buffer = String::with_capacity(config.max_width() * 2);
let mut b = 0;
for (c, b) in text.chars() {
for c in text.chars() {
b += 1;
if c == '\r' {
continue;
}
@ -456,8 +456,8 @@ fn format_lines(
}
if newline_count > 1 {
debug!("track truncate: {} {}", text.len, newline_count);
let line = text.len - newline_count + 1;
debug!("track truncate: {} {}", text.len(), newline_count);
let line = text.len() - newline_count + 1;
text.truncate(line);
}

View file

@ -17,12 +17,12 @@ use codemap::LineRangeUtils;
use comment::{rewrite_comment, CodeCharKind, CommentCodeSlices};
use config::WriteMode;
use shape::{Indent, Shape};
use utils::{count_newlines, mk_sp};
use utils::{count_newlines, last_line_width, mk_sp};
use visitor::FmtVisitor;
impl<'a> FmtVisitor<'a> {
fn output_at_start(&self) -> bool {
self.buffer.len == 0
self.buffer.len() == 0
}
// TODO these format_missing methods are ugly. Refactor and add unit tests
@ -86,7 +86,11 @@ impl<'a> FmtVisitor<'a> {
fn push_vertical_spaces(&mut self, mut newline_count: usize) {
// The buffer already has a trailing newline.
let offset = if self.buffer.cur_offset() == 0 { 0 } else { 1 };
let offset = if last_line_width(&self.buffer) == 0 {
0
} else {
1
};
let newline_upper_bound = self.config.blank_lines_upper_bound() + offset;
let newline_lower_bound = self.config.blank_lines_lower_bound() + offset;
if newline_count > newline_upper_bound {
@ -150,21 +154,21 @@ impl<'a> FmtVisitor<'a> {
}
if status.rewrite_next_comment {
if fix_indent {
let comment_indent = if fix_indent {
if let Some('{') = last_char {
self.push_str("\n");
}
let indent_str = self.block_indent.to_string(self.config);
self.push_str(&indent_str);
self.block_indent
} else {
self.push_str(" ");
}
Indent::from_width(self.config, last_line_width(&self.buffer))
};
let comment_width = ::std::cmp::min(
self.config.comment_width(),
self.config.max_width() - self.block_indent.width(),
);
let comment_indent = Indent::from_width(self.config, self.buffer.cur_offset());
let comment_shape = Shape::legacy(comment_width, comment_indent);
let comment_str = rewrite_comment(subslice, false, comment_shape, self.config)
.unwrap_or_else(|| String::from(subslice));

View file

@ -10,7 +10,6 @@
use std::cmp;
use strings::string_buffer::StringBuffer;
use syntax::{ast, visit};
use syntax::attr::HasAttrs;
use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
@ -74,7 +73,7 @@ impl<'a> SnippetProvider<'a> {
pub struct FmtVisitor<'a> {
pub parse_session: &'a ParseSess,
pub codemap: &'a CodeMap,
pub buffer: StringBuffer,
pub buffer: String,
pub last_pos: BytePos,
// FIXME: use an RAII util or closure for indenting
pub block_indent: Indent,
@ -252,7 +251,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
// The closing brace itself, however, should be indented at a shallower
// level.
fn close_block(&mut self, unindent_comment: bool) {
let total_len = self.buffer.len;
let total_len = self.buffer.len();
let chars_too_many = if unindent_comment {
0
} else if self.config.hard_tabs() {
@ -565,7 +564,7 @@ impl<'b, 'a: 'b> FmtVisitor<'a> {
FmtVisitor {
parse_session: parse_session,
codemap: parse_session.codemap(),
buffer: StringBuffer::new(),
buffer: String::with_capacity(snippet_provider.big_snippet.len() * 2),
last_pos: BytePos(0),
block_indent: Indent::empty(),
config: config,