June 23, 2011
Ok here's a little challenge for you fokes:

Write a D script that removes extra parens from strings like these:
    ("Acoustic Bass Drum"), ("Bass Drum 1"),
    ("Side Stick"), ("Acoustic Snare"),
    ("Hand Clap"), ("Electric Snare"),
    ("Low Floor Tom"), ("Closed High Hat"),
    ("High Floor Tom"), ("Pedal High Hat"),

and replaces it with:
    "Acoustic Bass Drum", "Bass Drum 1",
    "Side Stick", "Acoustic Snare",
    "Hand Clap", "Electric Snare",
    "Low Floor Tom", "Closed High Hat",
    "High Floor Tom", "Pedal High Hat",

I can't figure std.regex out. Every time I try to use the hit() property of a RegexMatch object I get back some weird internal error:

core.exception.AssertError@D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1861): 4294967295 .. 4294967295 vs. 2

The parens are leftover from code like this:
     TEXT ("Acoustic Bass Drum"), TEXT ("Bass Drum 1"),
     TEXT ("Side Stick"),         TEXT ("Acoustic Snare"),
     TEXT ("Hand Clap"),          TEXT ("Electric Snare"),
     TEXT ("Low Floor Tom"),      TEXT ("Closed High Hat"),
     TEXT ("High Floor Tom"),     TEXT ("Pedal High Hat"),

TEXT was just a macro that would probably add a function call if the C code was compiled with unicode support. I did a global search and replace of TEXT but that left me with a minefield of parens. :)

Maybe this will help: http://www.txt2re.com/index.php3?s=%28%22Acoustic+Bass+Drum%22%29&submit=Show+Matches
June 23, 2011
Andrej Mitrovic:

> Write a D script that removes extra parens from strings like these:
>     ("Acoustic Bass Drum"), ("Bass Drum 1"),
>     ("Side Stick"), ("Acoustic Snare"),
>     ("Hand Clap"), ("Electric Snare"),
>     ("Low Floor Tom"), ("Closed High Hat"),
>     ("High Floor Tom"), ("Pedal High Hat"),
> 
> and replaces it with:
>     "Acoustic Bass Drum", "Bass Drum 1",
>     "Side Stick", "Acoustic Snare",
>     "Hand Clap", "Electric Snare",
>     "Low Floor Tom", "Closed High Hat",
>     "High Floor Tom", "Pedal High Hat",
> 
> I can't figure std.regex out. Every time I try to use the hit() property of a RegexMatch object I get back some weird internal error:
> 
> core.exception.AssertError@D:\DMD\dmd2\windows\bin\..\..\src\phobos\std\regex.d(1861): 4294967295 .. 4294967295 vs. 2

I suggest to minimize the case and submit a bug report for Phobos.

Regarding your problem, I have written this Python code, probably it's not too much hard to translate it to D:

import re

lines = """
> The parens are leftover from code like this:
>      TEXT ("Acoustic Bass Drum"), TEXT ("Bass Drum 1"),
>      TEXT ("Side Stick"),         TEXT ("Acoustic Snare"),
>      TEXT ("Hand Clap"),          TEXT ("Electric Snare"),
>      TEXT ("Low Floor Tom"),      TEXT ("Closed High Hat"),
>      TEXT ("High Floor Tom"),     TEXT ("Pedal High Hat"),
""".splitlines()

patt = re.compile(r"""
TEXT \s*    \(
                \s*
                    (".*?")
                \s*
            \)  """, re.VERBOSE)

def replacer(m):
    return str(m.group(1))

for line in lines:
    print patt.sub(replacer, line)


I have used redemo2.pyw

Bye,
bearophile
June 23, 2011
What am I doing wrong here?

f1 = open('Drum.d', 'r')
lines = f1.readlines()
f1.close()

f2 = open('Drum.w', 'w')
for line in lines:
   #~ print(patt.sub(replacer, line))    # This replaces the parens correctly
   f2.write(patt.sub(replacer, line))     # But the file ends up being
the same..
f2.close()
June 23, 2011
On Wed, Jun 22, 2011 at 11:23 PM, Andrej Mitrovic < andrej.mitrovich@gmail.com> wrote:

> What am I doing wrong here?
>
> f1 = open('Drum.d', 'r')
> lines = f1.readlines()
> f1.close()
>
> f2 = open('Drum.w', 'w')
> for line in lines:
>   #~ print(patt.sub(replacer, line))    # This replaces the parens
> correctly
>   f2.write(patt.sub(replacer, line))     # But the file ends up being
> the same..
> f2.close()
>


That looks 100% right.


June 23, 2011
It works when I write to a different file:

f1 = open('Drum.d', 'r')
lines = f1.readlines()
f1.close()

f2 = open('Drum2.w', 'w')
for line in lines:
  #~ print(patt.sub(replacer, line))    # This replaces the parens correctly
  f2.write(patt.sub(replacer, line))     # But the file ends up being
the same..
f2.close()

Looks like some kind of file handling bug in Python?
June 23, 2011
Eh what the heck now it works again. LOL. Ok, well thanks bear for the script!
June 23, 2011
On Thu, Jun 23, 2011 at 12:25 AM, Andrej Mitrovic < andrej.mitrovich@gmail.com> wrote:

>
> Looks like some kind of file handling bug in Python?
>

On Thu, Jun 23, 2011 at 12:26 AM, Andrej Mitrovic < andrej.mitrovich@gmail.com> wrote:

> Eh what the heck now it works again. LOL. Ok, well thanks bear for the script!
>


 LOL. It's not that easy to find a bug in Python :P


June 23, 2011
Hmm it's a bit problematic though:

szLabel.indexOf("(")
szLabel.indexOf""

Well I'll just have to manually merge these changes then one by one. Well it's better than nothing.
June 23, 2011
This is why we need language-aware tools, it killed this:

throw new Exception("Can't allocate buffer.");

Woops:
throw new Exception"Can't allocate buffer.";
June 23, 2011
Fixed: https://github.com/AndrejMitrovic/DWindowsProgramming/commit/42228c9012c2c75d2638fab52277f21427b2ed01

Quite a huge list of stray parens. Good thing I had Beyond Compare for the diffs.