[PATCH v3] lua: add 'chars' param to string.strip functions

Vladimir Davydov vdavydov.dev at gmail.com
Thu Feb 21 16:01:34 MSK 2019


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.



More information about the Tarantool-patches mailing list