Skip to content

Commit

Permalink
Introduce convenient %gsub macro to wrap Lua's string.gsub()
Browse files Browse the repository at this point in the history
Examples:
  %{gsub 3.1~~dev1 ~} -> 3.1dev1
  %{gsub 2.3^post5 ^ -} -> 2.3-post5
  %{gsub foo_bar [^%%w]+ -} -> foo-bar
  %{gsub aaa a b 2} -> bba
  %{gsub dummy} -> dummy
  %{gsub} -> (empty string)

Related to rpm-software-management#1219
Related to https://src.fedoraproject.org/rpms/python-rpm-macros/pull-request/101
  • Loading branch information
hroncok committed Aug 26, 2021
1 parent fd57fc7 commit e168ef0
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions macros.in
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,32 @@ package or when debugging this package.\
%patches %{lua: for i, p in ipairs(patches) do print(p.." ") end}
%sources %{lua: for i, s in ipairs(sources) do print(s.." ") end}

#------------------------------------------------------------------------------
# Convenient string manipulation macros:
#
# gsub takes 3 arguments: a subject string, a pattern, and a replacement string
# If arguments are omitted, empty strings are assumed, as passing empty string is hard.
# Its basic use is to substitute the replacement string for all occurrences of
# the pattern inside the subject string.
# An optional fourth parameter limits the number of substitutions to be made.
# Lua pattern matching is used (in fact, the arguments are expanded and passed
# directly to the Lua's string.gsub function, returning the first return value).
# Examples:
# %%{gsub 3.1~~dev1 ~} -> 3.1dev1
# %%{gsub 2.3^post5 ^ -} -> 2.3-post5
# %%{gsub foo_bar [^%%%%w]+ -} -> foo-bar
# %%{gsub aaa a b 2} -> bba
# %%{gsub dummy} -> dummy
# %%{gsub} -> (empty string)
%gsub() %{lua:\
local subject = rpm.expand("%{?1}")\
local pattern = rpm.expand("%{?2}")\
local replacement = rpm.expand("%{?3}")\
local limit = rpm.expand("%{?4}")\
print((subject:gsub(pattern, replacement, limit ~= "" and limit or nil)))\
}


#------------------------------------------------------------------------------
# arch macro for all Intel i?86 compatible processors
# (Note: This macro (and it's analogues) will probably be obsoleted when
Expand Down

0 comments on commit e168ef0

Please sign in to comment.