Jump to content

Module:User:Erutuon/patterns/documentation

From Wiktionary, the free dictionary
Documentation for Module:User:Erutuon/patterns. [edit]
This page contains usage information, categories, interwiki links and other content describing the module.

Contains a function that determines if a pattern will behave in exactly the same way in both the basic Lua string functions (string.find, string.match, string.gsub, string.gmatch) and the Ustring functions (mw.ustring.find, mw.ustring.match, mw.ustring.gsub, mw.ustring.gmatch). This assumes text is validly encoded in UTF-8, the encoding used by MediaWiki.

Beware: the function tells you that a pattern requires the Ustring functions if it contains character classes ("%w, %s, %d, ...". But this is not always true. It depends what characters you actually need the character class to match. For example, if you're matching language codes, which only contain ASCII, then the character class "%a" only needs to match ASCII alphabetic characters ("[A-Za-z]" (not, for instance, Greek alphabetic characters "αβγδεζηθ..."), and the basic string function can be used.

Usage

[edit]

Don't use this in an actual template-invoked function to decide between string and mw.ustring on the fly. That would be very inefficient. If you want, use it to test a pattern and decide whether string will work just as well as mw.ustring in a given instance. Switching to string saves a noticeable amount of processing time if the function involves a lot of pattern-matching.

local UstringNotNeeded = require("Module:User:Erutuon/patterns").canUseString(pattern_to_check)
--> true if pattern will behave in string functions, false if it requires Ustring functions

The function log sends a message to the log if a mw.ustring function can be replaced by the corresponding string function.