From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Thu, 21 Feb 2019 16:01:34 +0300 From: Vladimir Davydov Subject: Re: [PATCH v3] lua: add 'chars' param to string.strip functions Message-ID: <20190221130134.l4wqw3tjtizl5mpr@esperanza> References: <20190204044727.nwtouqzg65mqngiv@tkn_work_nb> <20190218204232.zu72sokzqsiishvf@tkn_work_nb> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <20190218204232.zu72sokzqsiishvf@tkn_work_nb> To: Alexander Turenko Cc: =?utf-8?Q?Micha=C5=82?= Durak , "tarantool-patches@freelists.org" List-ID: On Mon, Feb 18, 2019 at 11:42:32PM +0300, Alexander Turenko wrote: > On Mon, Feb 11, 2019 at 07:20:38PM +0000, MichaƂ Durak wrote: > > Add optional 'chars' parameter to string.strip, string.lstrip > > and string.rstrip for specifying the unwanted characters. > > Behavior modeled after the equivalent Python built-ins. > > > > Closes: tarantool#2977 > > Does it work in that way? I know that `Closes: #2977` do, but don't sure > about your form. The documentation (the link is below) doesn't mention > this form as far as I see. Fixed it. > > +void > > +string_strip_helper(const char *inp, size_t inp_len, const char *chars, > > + size_t chars_len, bool lstrip, bool rstrip, > > + size_t *newstart, size_t *newlen) > > +{ > > + size_t skipped; > > + uint8_t arr[256] = {0}; > > + > > + for (size_t i = 0; i < chars_len; ++i) { > > + unsigned char c = chars[i]; > > + arr[c] = 1; > > + } > > + > > + *newstart = skipped = lstrip ? lstrip_helper(inp, inp_len, arr) : 0; > > + > > + if (rstrip) > > + skipped += rstrip_helper(inp + skipped, inp_len - skipped, arr); > > + > > + *newlen = inp_len - skipped; > > +} > > I think that the following way to write it looks simpler (marked changed > lines with !!): > > ``` > void > string_strip_helper(const char *inp, size_t inp_len, const char *chars, > size_t chars_len, bool lstrip, bool rstrip, > size_t *newstart, size_t *newlen) > { > !! size_t skipped = 0; > uint8_t arr[256] = {0}; > > for (size_t i = 0; i < chars_len; ++i) { > unsigned char c = chars[i]; > arr[c] = 1; > } > > !! if (lstrip) > !! skipped += lstrip_helper(inp, inp_len, arr); > > !! *newstart = skipped; > > if (rstrip) > skipped += rstrip_helper(inp + skipped, inp_len - skipped, arr); > > *newlen = inp_len - skipped; > } > ``` Agree. Updated the patch. > > +#if defined(__cplusplus) > > +extern "C" { > > +#endif /* defined(__cplusplus) */ > > + > > +/** \cond public */ > > + > > +void > > +string_strip_helper(const char *inp, size_t inp_len, const char *chars, > > + size_t chars_len, bool lstrip, bool rstrip, > > + size_t *newstart, size_t *newlen); > > + > > +/** \endcond public */ cond public isn't needed here. Removed. Pushed to 2.1.