~maphew/mindtouch-deki/dekiwiki

« back to all changes in this revision

Viewing changes to src/services/mindtouch.deki.script/Runtime/Library/DekiScriptLibrary-String.cs

  • Committer: steveb
  • Date: 2011-09-08 21:50:27 UTC
  • Revision ID: svn-v4:0eb84ffb-6e0e-0410-b475-cc1b69d517b7:public/dekiwiki/trunk:29477
Deki (trunk): merged from 29476
* MT-10676 string.matches does not respect groups correctly

Show diffs side-by-side

added added

removed removed

Lines of Context:
236
236
            [DekiScriptParam("string value")] string text,
237
237
            [DekiScriptParam("regular expression pattern")] string pattern,
238
238
            [DekiScriptParam("ignore case (default: false)", true)] bool? ignorecase,
239
 
            [DekiScriptParam("return text with index (default: false)", true)] bool? withIndex
 
239
            [DekiScriptParam("return text with index (default: false)", true)] bool? withIndex,
 
240
            [DekiScriptParam("return capture groups instead of matched text (default: false)", true)] bool? captures
240
241
        ) {
241
242
            ArrayList result = new ArrayList();
242
243
            for(var m = Regex.Match(text, pattern, (ignorecase ?? false) ? RegexOptions.IgnoreCase : RegexOptions.None); m.Success; m = m.NextMatch()) {
243
 
                for(var i = 1; i < m.Groups.Count; ++i) {
244
 
                    for(var j = 0; j < m.Groups[i].Captures.Count; ++j) {
245
 
                        if(withIndex.GetValueOrDefault()) {
246
 
                            var info = new Hashtable(StringComparer.OrdinalIgnoreCase);
247
 
                            info["text"] = m.Groups[i].Captures[j].Value;
248
 
                            info["index"] = m.Groups[i].Captures[j].Index;
249
 
                            result.Add(info);
250
 
                        } else {
251
 
                            result.Add(m.Groups[i].Captures[j].Value);
 
244
 
 
245
                // check if user wants to only return capture groups
 
246
                if(captures.GetValueOrDefault()) {
 
247
 
 
248
                    // skip over the first group since it represents the entire match
 
249
                    for(var i = 1; i < m.Groups.Count; ++i) {
 
250
                        var count = m.Groups[i].Captures.Count;
 
251
                        for(var j = 0; j < count; ++j) {
 
252
                            if(withIndex.GetValueOrDefault()) {
 
253
                                var info = new Hashtable(StringComparer.OrdinalIgnoreCase);
 
254
                                info["text"] = m.Groups[i].Captures[j].Value;
 
255
                                info["index"] = m.Groups[i].Captures[j].Index;
 
256
                                result.Add(info);
 
257
                            } else {
 
258
                                result.Add(m.Groups[i].Captures[j].Value);
 
259
                            }
252
260
                        }
253
261
                    }
 
262
                } else {
 
263
 
 
264
                    // check if matches should be returned with index positions
 
265
                    if(withIndex.GetValueOrDefault()) {
 
266
                        var info = new Hashtable(StringComparer.OrdinalIgnoreCase);
 
267
                        info["text"] = m.Value;
 
268
                        info["index"] = m.Index;
 
269
                        result.Add(info);
 
270
                    } else {
 
271
                        result.Add(m.Value);
 
272
                    }
254
273
                }
255
274
            }
256
275
            return result;