premake/util/sdl_string.lua
author Sam Lantinga
Thu, 01 Sep 2016 01:26:56 -0700
changeset 10304 ee83e0b4a36f
parent 9998 f67cf37e9cd4
permissions -rwxr-xr-x
wayland: Add support for relative mouse mode, by Jonas Ã…dahl <jadahl@gmail.com>

Generate the C protocol files from the protocol XML files installed by
wayland-protocols, and use them to implement support for relative pointer
motions and pointer locking.

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