sdl_string.lua (3245B)
1-- Copyright (C) 1997-2014 Sam Lantinga <slouken@libsdl.org> 2-- 3-- This software is provided 'as-is', without any express or implied 4-- warranty. In no event will the authors be held liable for any damages 5-- arising from the use of this software. 6-- 7-- Permission is granted to anyone to use this software for any purpose, 8-- including commercial applications, and to alter it and redistribute it 9-- freely. 10-- 11-- Meta-build system using premake created and maintained by 12-- Benjamin Henning <b.henning@digipen.edu> 13 14--[[ 15sdl_string.lua 16 17 Contains a few convenient string utility functions which are not supported in 18 Lua or not supported as intended. 19]] 20 21-- Performs a non-pattern based substring search of one string in another 22-- string. It's of O(n^2) complexity. It returns nil if the result cannot be 23-- found, otherwise it returns the starting index of the first found occurrence. 24string.indexOf = function(str, substr) 25 local pos = 1 26 local i = 1 27 for i = 1, str:len(), 1 do 28 if str:sub(i, i) == substr:sub(pos, pos) then 29 -- have we matched the complete string? 30 if pos == substr:len() then 31 return i - pos + 1-- starting pos 32 end 33 -- matched character...keep going 34 pos = pos + 1 35 else 36 -- restart, no match 37 pos = 0 38 end 39 end 40 if pos == substr:len() then 41 return i - pos + 1 42 end 43 return nil -- no match 44end 45 46-- This is a public-access version of the explode function defined below. 47function explode(str, delim) 48 return str:explode(delim) 49end 50 51-- Explodes a string into an array of elements, separated by a non-pattern 52-- delimiter. This function is part of the string table, allowing for a 53-- member-based invocation for strings. 54string.explode = function(str, delim) 55 local exploded = { } 56 local needle = string.find(str, delim) 57 while needle ~= nil do 58 table.insert(exploded, string.sub(str, 0, needle - 1)) 59 str = string.sub(str, needle + 1) 60 needle = string.find(str, delim) 61 end 62 table.insert(exploded, str) 63 return exploded 64end 65 66-- Similar to table.concat, except it supports more advanced token pasting. This 67-- function is vastly used by the main meta-build script (premake4.lua) to 68-- generate all the main lines of code for various tables that need to be in the 69-- generated lua file. 70-- - tbl: table of values to implode into a string 71-- - prefix: string to paste before entire result 72-- - pre: string to always paste before each entry in table 73-- - post: string to always paste after each entry in table 74-- - join: string to paste between entries (inclusive) 75-- - suffix: string to paste after entire result 76-- Returns the imploded string. 77function implode(tbl, prefix, pre, post, join, suffix) 78 local result = "" 79 -- not the most efficient way to do this, but... 80 local itbl = { } 81 for k,v in pairs(tbl) do 82 itbl[#itbl + 1] = v 83 end 84 for i = 1, #itbl, 1 do 85 if pre ~= nil then 86 result = result .. pre 87 end 88 result = result .. itbl[i] 89 if post ~= nil then 90 result = result .. post 91 end 92 if i ~= #itbl then 93 result = result .. join 94 end 95 end 96 if prefix ~= nil then 97 result = prefix .. result 98 end 99 if suffix ~= nil then 100 result = result .. suffix 101 end 102 return result 103end