Changes

Jump to navigation Jump to search
4,354 bytes added ,  16:58, 8 June 2020
m
no edit summary
[[Extensions_to_the_Lua_I/O_library|The I/O-Library]] >
 
=Overview=
The examples are so designed that they should work when copy-pasted into an
empty Lua file and processed with context.
 
=l-string.lua=
<tt>tabsize</tt> defaults to <em>7</em>.
==string.compactlong(string) | stringutilities.strings.striplong(string)==
<pre>
print(string.compactlong(s))
print(stringprintutilities.strings.striplong(s))
</pre>
leading spaces from <tt>string</tt>.
<tt>stringutilities.strings.striplong</tt> removes leading spaces and converts dos
newlines to unix newlines.
theory = [[All brontosauruses are thin at one end, much much thicker in the middle, and then thin again at the far end.]]
theorems = string.split(theory, lpeg.P", " * lpeg.P" and"^-1)
for n, element in ipairs(theorems) do
rest after that regardless of any further occurrences of <tt>separator</tt>.
= util-prs.lua =
 
== utilities.parsers.settings_to_hash(str) ==
 
<pre>
str = 'a=1, b=2, c=3'
utilities.parsers.settings_to_hash(str)
--> { a = 1, b = 2, c = 3 }
</pre>
 
{{code|utilities.parsers.settings_to_array}} takes a string of comma-separated key=value statements, and returns an associative array of {{code|1=["key"] = value}} entries. Very useful for parsing and accessing macro arguments at the Lua end.
 
== utilities.parsers.settings_to_array(str) ==
 
<pre>
str = 'top, inmargin=2, top, {here,now}'
utilities.parsers.settings_to_array(str)
--> { "top", "inmargin=2", "top", "here,now" }
</pre>
 
{{code|utilities.parsers.settings_to_array}} takes a string of comma-separated keywords, and returns a array of those keywords in the order in which they appear. Duplicates are not filtered. <tt>Key=value</tt> strings are taken as a single keyword. Surrounding braces are removed
 
== utilities.parsers.settings_to_set(str) ==
 
<pre>
str = 'top, inmargin=2, top, {here,now}'
utilities.parsers.settings_to_set(str)
--> { ["top"]=true, ["inmargin=2"]=true, ["here,now"]=true }
</pre>
 
{{code|utilities.parsers.settings_to_array}} takes a string of comma-separated keywords, and returns a array of those keywords in the order in which they appear, with duplicates removed.
<tt>Key=value</tt> strings are taken as a single keyword. Surrounding braces are removed
 
== Other function in utilities.parsers ==
{| class="wikitable"
|-
| utilities.parsers.add_settings_to_array
| parse and write directly into table
|-
| utilities.parsers.arguments_to_table
| parse arguments, return table
|-
| utilities.parsers.array_to_string(a,sep)
| concatenates a with custom sep or comma
|-
| utilities.parsers.getparameters
| write settings_to_hash to an array with a metatable. A metatable is a sort of parent: when a table is accessed, undefined values will be looked up in the metatable.
|-
| utilities.parsers.hash_to_string
| turn a hash into a string, with optional strictness settings
|-
| utilities.parsers.simple_hash_to_string
| concatenate the values of a hash
|-
| utilities.parsers.make_settings_to_hash_pattern
| returns parser pattern for strict, tolerant, or normal arg-parsing
|-
| utilities.parsers.settings_to_hash_strict
| like settings_to_hash with strict parsing
|-
| utilities.parsers.settings_to_hash_tolerant
| like settings_to_hash with tolerant parsing
|-
| utilities.parsers.splitthousands
| turns 12345678.44 into 12,345,678.44
|-
|}
=LuaTeX=
turned out to be almost twice as fast as an LPEG iterator.
=RecipiesRecipes=
General: [http://lua-users.org/wiki/StringRecipes string section of the Lua wiki].
<tt>words</tt> has the advantage that it allows for arbitrary patterns as
delimiters.
 
= String formatter =
 
The <code>context()</code> function uses its own formatter, of the form <code>context("something %Z something", object_formatted_by_Z)</code> Below is a table of the available formatting codes.
 
{| class="wikitable"
! result type || code || input type
|-
| integer || %...i || number
|-
| integer || %...d || number
|-
| unsigned || %...u || number
|-
| utf character || %...c || number
|-
| hexadecimal || %...x || number
|-
| HEXADECIMAL || %...X || number
|-
| octal || %...o || number
|-
| string || %...s || string, number
|-
| float || %...f || number
|-
| exponential || %...e || number
|-
| exponential || %...E || number
|-
| autofloat || %...g || number
|-
| autofloat || %...G || number
|-
| force tostring || %...S || any
|-
| force tostring || %Q || any
|-
| force tonumber || %N || number (strips leading zeros)
|-
| signed number || %I || number
|-
| rounded number || %r || number
|-
| 0xhexadecimal || %...h || character, number
|-
| 0xHEXADECIMAL || %...H || character, number
|-
| U+hexadecimal || %...u || character, number
|-
| U+HEXADECIMAL || %...U || character, number
|-
| points || %p || number in scaled points (65536sp = 1pt)
|-
| basepoints || %b || number in scaled points
|-
| table concat || %...t || table
|-
| true or false || %l || boolean
|-
| TRUE or FALSE || %L || boolean
|-
| ''number'' spaces || %...w || number
|-
| escaped XML || %!xml! || string
|-
| escaped TeX || %!tex! || string, number
|-
|}
 
[[Category:Programming and Databases]]

Navigation menu