MonoDevelop.Gettext.StringEscaping.FromGettextFormat C# (CSharp) Method

FromGettextFormat() public static method

public static FromGettextFormat ( string text ) : string
text string
return string
        public static string FromGettextFormat(string text)
        {
            StringBuilder sb = new StringBuilder ();
            for (int i = 0; i < text.Length; i++) {
                char c = text[i];
                switch (c) {
                case '\\':
                    if (i + 1 < text.Length) {
                        char nextChar = text [i + 1];
                        if (nextChar == '\\' || nextChar == '"') {
                            sb.Append (nextChar);
                            i++;
                            continue;
                        }
                        if (nextChar == 'n') {
                            sb.Append ('\n');
                            i++;
                            continue;
                        }
                        if (nextChar == 't') {
                            sb.Append ('\t');
                            i++;
                            continue;
                        }
                        if (nextChar == 'r') {
                            sb.Append ('\r');
                            i++;
                            continue;
                        }

                        throw new FormatException (String.Format ("Invalid escape sequence '{0}' in string: '{1}'",
                                                                  nextChar,
                                                                  text));
                    }
                    break;
                }

                sb.Append (c);
            }
            return sb.ToString ();
        }

Usage Example

Esempio n. 1
0
        // Initializes the headers from string that is in msgid "" format (i.e. list of key:value\n entries).
        public void ParseHeaderString(string headers)
        {
            string hdr = StringEscaping.FromGettextFormat(headers);

            string[] tokens = hdr.Split('\n');
            headerEntries.Clear();

            foreach (string token in tokens)
            {
                if (token != String.Empty)
                {
                    int pos = token.IndexOf(':');
                    if (pos == -1)
                    {
                        throw new Exception(String.Format("Malformed header: '{0}'", token));
                    }
                    else
                    {
                        string key   = token.Substring(0, pos).Trim();
                        string value = token.Substring(pos + 1).Trim();
                        headerEntries[key] = value;
                    }
                }
            }
            ParseHeaderDict();
        }
All Usage Examples Of MonoDevelop.Gettext.StringEscaping::FromGettextFormat