From 1dd5c499379aeb56e57c13c9c1531241cd6f8bc5 Mon Sep 17 00:00:00 2001 From: gwenn Date: Thu, 11 Feb 2016 21:19:18 +0100 Subject: [PATCH] Fix escape_double_quote --- src/vtab/csvtab.rs | 4 ++-- src/vtab/int_array.rs | 4 ++-- src/vtab/mod.rs | 8 +++++--- 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/vtab/csvtab.rs b/src/vtab/csvtab.rs index e1aa21c..b97b3d0 100644 --- a/src/vtab/csvtab.rs +++ b/src/vtab/csvtab.rs @@ -47,7 +47,7 @@ impl VTab for CSVTab { } let filename = try!(str::from_utf8(c_filename)); let mut reader = try!(csv::Reader::from_file(filename)).has_headers(false); // TODO flexible ? - let mut cols = Vec::new(); + let mut cols: Vec = Vec::new(); let args = &args[4..]; for c_arg in args { @@ -64,7 +64,7 @@ impl VTab for CSVTab { } else if uc.contains("NO_QUOTE") { reader = reader.quote(0); } else { - cols.push(escape_double_quote(String::from(arg))); + cols.push(escape_double_quote(arg).into_owned()); } } } diff --git a/src/vtab/int_array.rs b/src/vtab/int_array.rs index adad0a8..42b6356 100644 --- a/src/vtab/int_array.rs +++ b/src/vtab/int_array.rs @@ -13,13 +13,13 @@ pub fn create_int_array(conn: &Connection, name: &str) -> Result Result<()> { conn.execute_batch(&format!("DROP TABLE temp.\"{0}\"", - escape_double_quote(name.to_string()))) + escape_double_quote(name))) } init_module!(INT_ARRAY_MODULE, IntArrayVTab, IntArrayVTabCursor, diff --git a/src/vtab/mod.rs b/src/vtab/mod.rs index b7b2cde..e7c9887 100644 --- a/src/vtab/mod.rs +++ b/src/vtab/mod.rs @@ -1,5 +1,7 @@ //! Create virtual tables. //! (See http://sqlite.org/vtab.html) +use std::borrow::Cow; +use std::borrow::Cow::{Borrowed, Owned}; use std::ffi::CString; use std::mem; use std::ptr; @@ -128,12 +130,12 @@ pub fn declare_vtab(db: *mut ffi::sqlite3, sql: &str) -> Result<()> { } /// Escape double-quote (`"`) character occurences by doubling them (`""`). -pub fn escape_double_quote(identifier: String) -> String { +pub fn escape_double_quote<'a>(identifier: &'a str) -> Cow<'a, str> { if identifier.contains('"') { // escape quote by doubling them - identifier.replace("\"", "\"\"") + Owned(identifier.replace("\"", "\"\"")) } else { - identifier + Borrowed(identifier) } }