3

I recently had to translate propkeys.h (in C[++]?) in C#.

My goal was to come from:

DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7);

To:

public static PropertyKey Audio_ChannelCount = new PropertyKey(new Guid("{64440490-4C8B-11D1-8B70-080036B11A03}"));

I was using Notepad++ for the regex, but I'm open to any other scriptable solution (perl, sed...). Please no compiled language (as C#, Java...).

I ended up with this (working):

// TURNS GUID into String
// Find what (Line breaks inserted for convenience):
0x([[:xdigit:]]{8}),\s*0x([[:xdigit:]]{4}),\s*0x([[:xdigit:]]
{4}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]
{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]
{2}),\s*0x([[:xdigit:]]{2}),\s*0x([[:xdigit:]]{2})

// Replace with:
new Guid\("{$1-$2-$3-$4$5-$6$7$8$9$10$11}"\)

// Final pass
// Find what:
^DEFINE_PROPERTYKEY\(PKEY_(\w+),\s*(new Guid\("\{[[:xdigit:]|\-]+"\)),\s*\d+\);$
// Replace with:
public static PropertyKey $1 = new PropertyKey\($2\);

While this is working, I feel the first pass something odd. I wanted to replace the tons of {2} with just a repeating one. Something like:

(0x([[:xdigit:]]){2},\s*)+

But can't get it to work with groups. Can someone tell me a "standard" way of doing this with regexes ?

4

1 回答 1

0

不幸的是,当您使用量词执行匹配时,该组将匹配整个文本,因此更“优雅”的解决方案是使用 perl 的 \G 元字符的等效项,它在前一个匹配结束后开始匹配。你可以使用这样的东西(Perl):

my $text = "DEFINE_PROPERTYKEY(PKEY_Audio_ChannelCount, 0x64440490, 0x4C8B, 0x11D1, 0x8B, 0x70, 0x08, 0x00, 0x36, 0xB1, 0x1A, 0x03, 7);";
my $res = "public static PropertyKey Audio_ChannelCount = new PropertyKey(new Guid(\"{";

if($text =~ m/0x((?:\d|[A-F]){8}),\s*0x((?:\d|[A-F]){4}),\s*0x((?:\d|[A-F]){4})/gc)
{
   $res .= $1 . "-" . $2 . "-" . $3 . "-";
}

if($text =~ m/\G,\s*0x((?:\d|[A-F]){2}),\s*0x((?:\d|[A-F]){2})/gc)#
{
   $res .= $1 . $2 . "-";
}

while($text =~ m/\G,\s*0x((?:\d|[A-F]){2})/gc)
{
   $res .= $1;
}

$res .= "}\"))";

print $res . "\n";

之后,您应该在 $res 上有结果字符串。运行此脚本时我的输出是:

public static PropertyKey Audio_ChannelCount = new PropertyKey(new Guid("{64440490-4C8B-11D1-8B70-080036B11A03}"))

免责声明:我不是 Perl 程序员,所以如果此代码中有任何实质性错误,请随时纠正它们

于 2013-07-31T07:32:39.843 回答