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