~mna/tulip

The tulip.xstring module contains string functions that extend Lua's standard string module.

#API

Assuming local xstring = require 'tulip.xstring'.

#s = xstring.capitalize(s)

Make each first letter of each word in s uppercase, the rest lowercase.

#t = xstring.decode_header(h, name)

Decodes header value h into an array where each entry is a table with a field "value" set to that entry's value, and an arbitrary number of other fields corresponding to that header value's directive (a semicolon-separated series of key=value after the header's value). If a directive appears without a value (e.g. key only), it is set as a key on the table with the value set to true.

It returns an array because headers can specify multiple values using a comma-separated list.

For example (Accept):

  text/html, application/xhtml+xml, application/xml;q=0.9, image/webp, */*;q=0.8

Would return:

  {
    {value = 'text/html'},
    {value = 'application/xhtml+xml'},
    {value = 'application/xml', q = '0.9'},
    {value = 'image/webp', q = '0.9'},
    {value = '*/*', q = '0.8'},
  }

Another example (Content-Type):

  multipart/form-data; boundary=something

Would return:

  {
    {value = 'multipart/form-data', boundary = 'something'},
  }

Finally (Accept-Encoding):

  deflate, gzip;q=1.0, *;q=0.5

Would return:

  {
    {value = 'deflate'},
    {value = 'gzip', q = '1.0'},
    {value = '*', q = '0.5'},
  }

If the optional name parameter is provided, it is set as the field name on the array itself, to keep track of what header this table represents.

#s = xstring.escapefile(s)

Escape s to return a valid file name, replacing potentially unsafe characters with underscores.

#indices, ht = xstring.header_directive_matches(h, d, v, plain)

Tests if header value h (which may have multiple values) has a directive d that matches value v. If h is a string, it is first decoded by calling decode_header. If v is a table, it is considered an array of string values to test. If plain is falsy, v is considered a Lua pattern.

If at least one match is found, it returns a table that is an array of indices where a match was found, in the decoded header's array. The second return value is the decoded header's array. Returns nil if no match is found.

#indices, ht = xstring.header_value_matches(h, v, plain)

Tests if header value h (which may have multiple values) matches value v. If h is a string, it is first decoded by calling decode_header. If v is a table, it is considered an array of string values to test. If plain is falsy, v is considered a Lua pattern.

If at least one match is found, it returns a table that is an array of indices where a match was found in the decoded header's array. The second return value is the decoded header's array. Returns nil if no match is found.

#s = xstring.ipat(pat)

Turn pat into a case-insensitive pattern. It throws an error if pat contains a set, which is not supported.

#s = xstring.normalizews(s)

Ensure all whitespace in s is normalized to a single space character.

#s, err = xstring.totime(s)

Parse a time in string RFC-3339 format to an epoch value. Return nil and an error if s is not a valid RFC-3339 format.

#s = xstring.trim(s)

Trim leading and trailing whitespace from s.

Back to index

About this wiki

commit 3ebfbd288b8e5c95fdf8ce2027a0e94cfa1c8976
Author: Martin Angers <martin.n.angers@gmail.com>
Date:   2021-02-25T14:07:12-05:00

Update to reflect Request:validate_body
Clone this wiki
https://git.sr.ht/~mna/tulip-wiki (read-only)
git@git.sr.ht:~mna/tulip-wiki (read/write)