move String instead of re-alloc

pull/14/head
chris west 4 years ago
parent 9c64f7c420
commit 89291da67b

@ -113,7 +113,7 @@ impl View for Menu {
impl Menu { impl Menu {
/// Create a representation of a Gopher Menu from a raw Gopher /// Create a representation of a Gopher Menu from a raw Gopher
/// response and a few options. /// response and a few options.
pub fn from(url: &str, response: &str, tls: bool, tor: bool) -> Menu { pub fn from(url: &str, response: String, tls: bool, tor: bool) -> Menu {
Menu { Menu {
tls, tls,
tor, tor,
@ -760,7 +760,7 @@ impl Menu {
} }
/// Parse gopher response into a Menu object. /// Parse gopher response into a Menu object.
pub fn parse(url: &str, raw: &str) -> Menu { pub fn parse(url: &str, raw: String) -> Menu {
let mut lines = vec![]; let mut lines = vec![];
let mut links = vec![]; let mut links = vec![];
let mut longest = 0; let mut longest = 0;
@ -788,7 +788,7 @@ impl Menu {
lines, lines,
links, links,
longest, longest,
raw: url.into(), raw,
input: String::new(), input: String::new(),
link: 0, link: 0,
mode: Default::default(), mode: Default::default(),
@ -866,7 +866,7 @@ mod tests {
macro_rules! parse { macro_rules! parse {
($s:literal) => { ($s:literal) => {
Menu::parse("test", $s); Menu::parse("test", $s.to_string());
}; };
} }

@ -161,7 +161,7 @@ impl View for Text {
impl Text { impl Text {
/// Create a Text View from a raw Gopher response and a few options. /// Create a Text View from a raw Gopher response and a few options.
pub fn from(url: &str, response: &str, tls: bool, tor: bool) -> Text { pub fn from(url: &str, response: String, tls: bool, tor: bool) -> Text {
let mut lines = 0; let mut lines = 0;
let mut longest = 0; let mut longest = 0;
for line in response.split_terminator('\n') { for line in response.split_terminator('\n') {
@ -173,7 +173,7 @@ impl Text {
Text { Text {
url: url.into(), url: url.into(),
raw_response: response.into(), raw_response: response,
scroll: 0, scroll: 0,
lines, lines,
longest, longest,

@ -246,8 +246,8 @@ impl UI {
}; };
let (typ, _, _, _) = gopher::parse_url(&url); let (typ, _, _, _) = gopher::parse_url(&url);
match typ { match typ {
Type::Menu | Type::Search => Ok(Box::new(Menu::from(url, &res, tls, tor))), Type::Menu | Type::Search => Ok(Box::new(Menu::from(url, res, tls, tor))),
Type::Text | Type::HTML => Ok(Box::new(Text::from(url, &res, tls, tor))), Type::Text | Type::HTML => Ok(Box::new(Text::from(url, res, tls, tor))),
_ => Err(error!("Unsupported Gopher Response: {:?}", typ)), _ => Err(error!("Unsupported Gopher Response: {:?}", typ)),
} }
} }
@ -258,7 +258,7 @@ impl UI {
&url.trim_start_matches("gopher://phetch/") &url.trim_start_matches("gopher://phetch/")
.trim_start_matches("1/"), .trim_start_matches("1/"),
) { ) {
Ok(Box::new(Menu::from(url, &source, false, false))) Ok(Box::new(Menu::from(url, source, false, false)))
} else { } else {
Err(error!("phetch URL not found: {}", url)) Err(error!("phetch URL not found: {}", url))
} }
@ -584,7 +584,7 @@ impl UI {
'r' => { 'r' => {
if let Some(page) = self.views.get(self.focused) { if let Some(page) = self.views.get(self.focused) {
let url = page.url(); let url = page.url();
let raw = page.raw(); let raw = page.raw().to_string();
let mut text = Text::from(url, raw, page.is_tls(), page.is_tor()); let mut text = Text::from(url, raw, page.is_tls(), page.is_tor());
text.wide = true; text.wide = true;
self.add_page(Box::new(text)); self.add_page(Box::new(text));

Loading…
Cancel
Save