premake/util/sdl_string.lua
changeset 7925 f090a47eb7f7
child 8149 681eb46b8ac4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/premake/util/sdl_string.lua	Sun Nov 10 00:38:37 2013 -0500
     1.3 @@ -0,0 +1,103 @@
     1.4 +-- Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.5 +--
     1.6 +-- This software is provided 'as-is', without any express or implied
     1.7 +-- warranty.  In no event will the authors be held liable for any damages
     1.8 +-- arising from the use of this software.
     1.9 +--
    1.10 +-- Permission is granted to anyone to use this software for any purpose,
    1.11 +-- including commercial applications, and to alter it and redistribute it
    1.12 +-- freely.
    1.13 +--
    1.14 +-- Meta-build system using premake created and maintained by
    1.15 +-- Benjamin Henning <b.henning@digipen.edu>
    1.16 +
    1.17 +--[[
    1.18 +sdl_string.lua
    1.19 +
    1.20 +	Contains a few convenient string utility functions which are not supported in
    1.21 +	Lua or not supported as intended.
    1.22 +]]
    1.23 +
    1.24 +-- Performs a non-pattern based substring search of one string in another
    1.25 +-- string. It's of O(n^2) complexity. It returns nil if the result cannot be
    1.26 +-- found, otherwise it returns the starting index of the first found occurrence.
    1.27 +string.indexOf = function(str, substr)
    1.28 +	local pos = 1
    1.29 +	local i = 1
    1.30 +	for i = 1, str:len(), 1 do
    1.31 +		if str:sub(i, i) == substr:sub(pos, pos) then
    1.32 +			-- have we matched the complete string?
    1.33 +			if pos == substr:len() then
    1.34 +				return i - pos + 1-- starting pos
    1.35 +			end
    1.36 +			-- matched character...keep going
    1.37 +			pos = pos + 1
    1.38 +		else
    1.39 +			-- restart, no match
    1.40 +			pos = 0
    1.41 +		end
    1.42 +	end
    1.43 +	if pos == substr:len() then
    1.44 +		return i - pos + 1
    1.45 +	end
    1.46 +	return nil -- no match
    1.47 +end
    1.48 +
    1.49 +-- This is a public-access version of the explode function defined below.
    1.50 +function explode(str, delim)
    1.51 +	return str:explode(delim)
    1.52 +end
    1.53 +
    1.54 +-- Explodes a string into an array of elements, separated by a non-pattern
    1.55 +-- delimiter. This function is part of the string table, allowing for a
    1.56 +-- member-based invocation for strings.
    1.57 +string.explode = function(str, delim)
    1.58 +	local exploded = { }
    1.59 +	local needle = string.find(str, delim)
    1.60 +	while needle ~= nil do
    1.61 +		table.insert(exploded, string.sub(str, 0, needle - 1))
    1.62 +		str = string.sub(str, needle + 1)
    1.63 +		needle = string.find(str, delim)
    1.64 +	end
    1.65 +	table.insert(exploded, str)
    1.66 +	return exploded
    1.67 +end
    1.68 +
    1.69 +-- Similar to table.concat, except it supports more advanced token pasting. This
    1.70 +-- function is vastly used by the main meta-build script (premake4.lua) to
    1.71 +-- generate all the main lines of code for various tables that need to be in the
    1.72 +-- generated lua file.
    1.73 +--  - tbl: table of values to implode into a string
    1.74 +--  - prefix: string to paste before entire result
    1.75 +--  - pre: string to always paste before each entry in table
    1.76 +--  - post: string to always paste after each entry in table
    1.77 +--  - join: string to paste between entries (inclusive)
    1.78 +--  - suffix: string to paste after entire result
    1.79 +-- Returns the imploded string.
    1.80 +function implode(tbl, prefix, pre, post, join, suffix)
    1.81 +	local result = ""
    1.82 +	-- not the most efficient way to do this, but...
    1.83 +	local itbl = { }
    1.84 +	for k,v in pairs(tbl) do
    1.85 +		itbl[#itbl + 1] = v
    1.86 +	end
    1.87 +	for i = 1, #itbl, 1 do
    1.88 +		if pre ~= nil then
    1.89 +			result = result .. pre
    1.90 +		end
    1.91 +		result = result .. itbl[i]
    1.92 +		if post ~= nil then
    1.93 +			result = result .. post
    1.94 +		end
    1.95 +		if i ~= #itbl then
    1.96 +			result = result .. join
    1.97 +		end
    1.98 +	end
    1.99 +	if prefix ~= nil then
   1.100 +		result = prefix .. result
   1.101 +	end
   1.102 +	if suffix ~= nil then
   1.103 +		result = result .. suffix
   1.104 +	end
   1.105 +	return result
   1.106 +end
   1.107 \ No newline at end of file