~ubuntu-branches/ubuntu/oneiric/monodevelop/oneiric

« back to all changes in this revision

Viewing changes to contrib/Sharpen/Sharpen/MessageFormat.cs

  • Committer: Bazaar Package Importer
  • Author(s): Jo Shields
  • Date: 2011-06-27 17:03:13 UTC
  • mto: (1.8.1 upstream)
  • mto: This revision was merged to the branch mainline in revision 54.
  • Revision ID: james.westby@ubuntu.com-20110627170313-6cvz3s19x6e9hqe9
ImportĀ upstreamĀ versionĀ 2.5.92+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
using System.Text;
 
2
using System.Collections.Generic;
 
3
 
 
4
namespace Sharpen
 
5
{
 
6
        using System;
 
7
 
 
8
        internal class MessageFormat
 
9
        {
 
10
                public static string Format (string message, params object[] args)
 
11
                {
 
12
                        StringBuilder sb = new StringBuilder ();
 
13
                        bool inQuote = false;
 
14
                        bool inPlaceholder = false;
 
15
                        int argStartPos = 0;
 
16
                        List<string> placeholderArgs = new List<string> (3);
 
17
                        
 
18
                        for (int n=0; n<message.Length; n++) {
 
19
                                char c = message[n];
 
20
                                if (c == '\'') {
 
21
                                        if (!inQuote)
 
22
                                                inQuote = true;
 
23
                                        else if (n > 0 && message [n-1] == '\'') {
 
24
                                                inQuote = false;
 
25
                                        }
 
26
                                        else {
 
27
                                                inQuote = false;
 
28
                                                continue;
 
29
                                        }
 
30
                                }
 
31
                                else if (c == '{' && !inQuote) {
 
32
                                        inPlaceholder = true;
 
33
                                        argStartPos = n + 1;
 
34
                                        continue;
 
35
                                }
 
36
                                else if (c == '}' && !inQuote && inPlaceholder) {
 
37
                                        inPlaceholder = false;
 
38
                                        placeholderArgs.Add (message.Substring (argStartPos, n - argStartPos));
 
39
                                        AddFormatted (sb, placeholderArgs, args);
 
40
                                        placeholderArgs.Clear ();
 
41
                                        continue;
 
42
                                }
 
43
                                else if (c == ',' && inPlaceholder) {
 
44
                                        placeholderArgs.Add (message.Substring (argStartPos, n - argStartPos));
 
45
                                        argStartPos = n + 1;
 
46
                                        continue;
 
47
                                }
 
48
                                else if (inPlaceholder)
 
49
                                        continue;
 
50
                                
 
51
                                sb.Append (c);
 
52
                        }
 
53
                        return sb.ToString ();
 
54
                }
 
55
 
 
56
                static void AddFormatted (StringBuilder sb, List<string> placeholderArgs, object[] args)
 
57
                {
 
58
                        if (placeholderArgs.Count > 3)
 
59
                                throw new ArgumentException ("Invalid format pattern: {" + string.Join (",", placeholderArgs.ToArray()) + "}");
 
60
                                
 
61
                        int narg;
 
62
                        if (!int.TryParse (placeholderArgs[0], out narg))
 
63
                                throw new ArgumentException ("Invalid argument index: " + placeholderArgs[0]);
 
64
                        if (narg < 0 || narg >= args.Length)
 
65
                                throw new ArgumentException ("Invalid argument index: " + narg);
 
66
                        
 
67
                        object arg = args [narg];
 
68
                        sb.Append (arg);
 
69
                        
 
70
                        // TODO: handle format types and styles
 
71
                }
 
72
        }
 
73
}