I have a parameter list and want to replace type "GFile00Array" with "ptr GFile00". One solution is
import strutils
proc test(str: string) =
var all = str
echo all
if all.find("00Array") >= 0:
var h = all.split(';')
for el in mitems(h):
var a, b: string
(a, b) = el.split(": ")
if b.find("Array") >= 0:
b = "ptr " & b.replace("Array")
el = a & ": " & b
all = h.join(";")
echo all
test("(self: ptr GApplication00; files: GFile00Array; nFiles: int32; hint: cstring; user_data: pointer)")
which gives the desired result:
$ ./t
(self: ptr GApplication00; files: GFile00Array; nFiles: int32; hint: cstring; user_data: pointer)
(self: ptr GApplication00; files: ptr GFile00; nFiles: int32; hint: cstring; user_data: pointer)
Are there better solutions? I think in Ruby I may have used regex pattern matching, as it was built in for Ruby. I have never tried pattern matching in Nim, maybe there exist other solutions?
PS: Indeed these marking of data types with suffixes is a bit ugly, I am working on removing it in most of the code, but there is one location where it is really very useful, and so it not fully removed yet.
Strutils.tokenize
Oh yes, tokenize() is interesting, I never noticed it before. Will try soon.
here you go Stefan_Salewski
import strutils
proc transform(str: string): string =
const matcher = "Array"
for (elem, isSep) in str.tokenize(Whitespace + {',', ';'}):
if elem.endsWith(matcher): result.add "ptr " & elem[0 .. ^(matcher.len + 1)]
else: result.add elem
when isMainModule:
import unittest
suite "t6775":
test "transform":
block:
let raw = "(self: ptr GApplication00; files: GFile00Array; nFiles: int32; hint: cstring; user_data: pointer)"
let trans = "(self: ptr GApplication00; files: ptr GFile00; nFiles: int32; hint: cstring; user_data: pointer)"
check raw.transform() == trans
block:
let raw = "(self: ptr GApplication00, files: GFile00Array, nFiles: int32, hint: cstring, user_data: pointer)"
let trans = "(self: ptr GApplication00, files: ptr GFile00, nFiles: int32, hint: cstring, user_data: pointer)"
check raw.transform() == trans