Home
Manage Your Code
Snippet: Parser pascal notation (C#)
Title: Parser pascal notation Language: C#
Description: Parser Views: 795
Author: david vílchez Date Added: 6/29/2007
Copy Code  
1/// <summary>

2        /// Pasar a notación Pascal.

3        /// </summary>

4        /// <param name="sentencia">The sentencia.</param>

5        /// <returns></returns>

6        public static string PascalNotation(string sentencia)
7        {
8            string tipoA = "^[a-z]*";
9            string tipoB = "[A-Z]{1}[a-z]{1,}";
10            string tipoC = "[A-Z]{1,}$|[A-Z]{1,}(?=[A-Z])";
11            string tipoD = "_{1,}([a-z]{1,}|[A-Z]{1,}(?=[A-Z]))";
12
13            if (!String.IsNullOrEmpty(Regex.Match(sentencia, tipoA).Value))
14            {
15                sentencia = sentencia.Replace(Regex.Match(sentencia, tipoA).Value,
16                    MSCapitalize(Regex.Match(sentencia, tipoA).Value));
17            }
18            foreach (Match captura in Regex.Matches(sentencia, tipoB))
19            {
20                sentencia = sentencia.Replace(captura.Value, MSCapitalize(captura.Value));
21            }
22            foreach (Match captura in Regex.Matches(sentencia, tipoC))
23            {
24                sentencia = sentencia.Replace(captura.Value, MSCapitalize(captura.Value));
25            }
26            foreach (Match captura in Regex.Matches(sentencia, tipoD))
27            {
28                sentencia = sentencia.Replace(captura.Value, MSCapitalize(captura.Value.Replace("_", "")));
29            }
30            //Hack

31            return sentencia.Replace("_", "");
32        }
33
34
35        /// <summary>

36        /// Capitalizar a notación MS.

37        /// </summary>

38        /// <param name="str">The STR.</param>

39        /// <returns></returns>

40        public static string MSCapitalize(string str)
41        {
42            if (str.Length > 2 || str=="Id")
43                return char.ToUpper(str[0]) + str.Substring(1).ToLower();
44            else
45                return str.ToUpper();
46        }
Usage
Generar cadenas que validen la notación Pascal, por jemplo para fxcop.
Notes
EN DESARROLLO en principio funciona pero hay que refinarlo y refactorizarlo