Date: Thu, 23 Mar 2017 19:58:53 +0100 From: Roman Divacky <rdivacky@freebsd.org> To: Warner Losh <imp@FreeBSD.org> Cc: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r315773 - head/sbin/devd Message-ID: <20170323185853.GA87388@vlakno.cz> In-Reply-To: <201703230236.v2N2apvX051799@repo.freebsd.org> References: <201703230236.v2N2apvX051799@repo.freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Thu, Mar 23, 2017 at 02:36:51AM +0000, Warner Losh wrote: > Author: imp > Date: Thu Mar 23 02:36:51 2017 > New Revision: 315773 > URL: https://svnweb.freebsd.org/changeset/base/315773 > > Log: > Implement quote escaping. String values may now contain " if you > it is preceded by \. > > foo="I \"like\" C++" > > gives the value 'I "like" C++' to the variable 'foo'. If a character > other than " follows the \, both the \ and that character are passed > through. > > Differential Revision: https://reviews.freebsd.org/D6286 > Sponsored by: Netflix > > Modified: > head/sbin/devd/devd.cc > head/sbin/devd/devd.hh > > Modified: head/sbin/devd/devd.cc > ============================================================================== > --- head/sbin/devd/devd.cc Thu Mar 23 02:33:27 2017 (r315772) > +++ head/sbin/devd/devd.cc Thu Mar 23 02:36:51 2017 (r315773) > @@ -411,6 +411,32 @@ var_list::is_set(const string &var) cons > return (_vars.find(var) != _vars.end()); > } > > +/** fix_value > + * > + * Removes quoted characters that have made it this far. \" are > + * converted to ". For all other characters, both \ and following > + * character. So the string 'fre\:\"' is translated to 'fred\:"'. > + */ > +const std::string & > +var_list::fix_value(const std::string &val) const > +{ > + char *tmp, *dst; > + const char *src; > + std::string *rv; > + > + dst = tmp = new char[val.length()]; > + src = val.c_str(); > + while (*src) { > + if (*src == '\\' && src[1] == '"') > + src++; > + else > + *dst++ = *src++; > + } > + rv = new string(tmp); > + delete tmp; > + return *rv; > +} Can the temporary char[] be stack allocated? Also, when returning a reference to a heap allocated string who is responsible for freeing it? Perhaps you can just return std::string and let the compiler optimize it? Roman
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20170323185853.GA87388>