Fixing an issue with output parameters that are of type IntPtr
[castle.git] / ActiveRecord / Castle.ActiveRecord / Framework / Internal / Inflector.cs
blob3e3fe2c8e454d0e8d7f5e184ed0cd7080cde0476
1 // Copyright 2004-2008 Castle Project - http://www.castleproject.org/
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle.ActiveRecord.Framework.Internal
17 using System.Collections;
18 using System.Text.RegularExpressions;
20 /// <summary>
21 /// The Inflector class transforms words from one
22 /// form to another. For example, from singular to plural.
23 /// </summary>
24 public class Inflector
26 private static readonly ArrayList plurals = new ArrayList();
27 private static readonly ArrayList singulars = new ArrayList();
28 private static readonly ArrayList uncountables = new ArrayList();
30 private Inflector()
34 #region Default Rules
36 static Inflector()
38 AddPlural("$", "s");
39 AddPlural("s$", "s");
40 AddPlural("(ax|test)is$", "$1es");
41 AddPlural("(octop|vir)us$", "$1i");
42 AddPlural("(alias|status)$", "$1es");
43 AddPlural("(bu)s$", "$1ses");
44 AddPlural("(buffal|tomat)o$", "$1oes");
45 AddPlural("([ti])um$", "$1a");
46 AddPlural("sis$", "ses");
47 AddPlural("(?:([^f])fe|([lr])f)$", "$1$2ves");
48 AddPlural("(hive)$", "$1s");
49 AddPlural("([^aeiouy]|qu)y$", "$1ies");
50 AddPlural("(x|ch|ss|sh)$", "$1es");
51 AddPlural("(matr|vert|ind)ix|ex$", "$1ices");
52 AddPlural("([m|l])ouse$", "$1ice");
53 AddPlural("^(ox)$", "$1en");
54 AddPlural("(quiz)$", "$1zes");
56 AddSingular("s$", "");
57 AddSingular("(n)ews$", "$1ews");
58 AddSingular("([ti])a$", "$1um");
59 AddSingular("((a)naly|(b)a|(d)iagno|(p)arenthe|(p)rogno|(s)ynop|(t)he)ses$", "$1$2sis");
60 AddSingular("(^analy)ses$", "$1sis");
61 AddSingular("([^f])ves$", "$1fe");
62 AddSingular("(hive)s$", "$1");
63 AddSingular("(tive)s$", "$1");
64 AddSingular("([lr])ves$", "$1f");
65 AddSingular("([^aeiouy]|qu)ies$", "$1y");
66 AddSingular("(s)eries$", "$1eries");
67 AddSingular("(m)ovies$", "$1ovie");
68 AddSingular("(x|ch|ss|sh)es$", "$1");
69 AddSingular("([m|l])ice$", "$1ouse");
70 AddSingular("(bus)es$", "$1");
71 AddSingular("(o)es$", "$1");
72 AddSingular("(shoe)s$", "$1");
73 AddSingular("(cris|ax|test)es$", "$1is");
74 AddSingular("(octop|vir)i$", "$1us");
75 AddSingular("(alias|status)es$", "$1");
76 AddSingular("^(ox)en", "$1");
77 AddSingular("(vert|ind)ices$", "$1ex");
78 AddSingular("(matr)ices$", "$1ix");
79 AddSingular("(quiz)zes$", "$1");
81 AddIrregular("person", "people");
82 AddIrregular("man", "men");
83 AddIrregular("child", "children");
84 AddIrregular("sex", "sexes");
85 AddIrregular("move", "moves");
87 AddUncountable("equipment");
88 AddUncountable("information");
89 AddUncountable("rice");
90 AddUncountable("money");
91 AddUncountable("species");
92 AddUncountable("series");
93 AddUncountable("fish");
94 AddUncountable("sheep");
97 #endregion
99 #region Rule inner class
101 private class Rule
103 private readonly Regex regex;
104 private readonly string replacement;
106 public Rule(string pattern, string replacement)
108 regex = new Regex(pattern, RegexOptions.IgnoreCase);
109 this.replacement = replacement;
112 public string Apply(string word)
114 if (!regex.IsMatch(word))
116 return null;
119 return regex.Replace(word, replacement);
123 #endregion
125 /// <summary>
126 /// Return the plural of a word.
127 /// </summary>
128 /// <param name="word">The singular form</param>
129 /// <returns>The plural form of <paramref name="word"/></returns>
130 public static string Pluralize(string word)
132 return ApplyRules(plurals, word);
135 /// <summary>
136 /// Return the singular of a word.
137 /// </summary>
138 /// <param name="word">The plural form</param>
139 /// <returns>The singular form of <paramref name="word"/></returns>
140 public static string Singularize(string word)
142 return ApplyRules(singulars, word);
145 /// <summary>
146 /// Capitalizes a word.
147 /// </summary>
148 /// <param name="word">The word to be capitalized.</param>
149 /// <returns><paramref name="word"/> capitalized.</returns>
150 public static string Capitalize(string word)
152 return word.Substring(0, 1).ToUpper() + word.Substring(1).ToLower();
155 private static void AddIrregular(string singular, string plural)
157 AddPlural("(" + singular[0] + ")" + singular.Substring(1) + "$", "$1" + plural.Substring(1));
158 AddSingular("(" + plural[0] + ")" + plural.Substring(1) + "$", "$1" + singular.Substring(1));
161 private static void AddUncountable(string word)
163 uncountables.Add(word.ToLower());
166 private static void AddPlural(string rule, string replacement)
168 plurals.Add(new Rule(rule, replacement));
171 private static void AddSingular(string rule, string replacement)
173 singulars.Add(new Rule(rule, replacement));
176 private static string ApplyRules(IList rules, string word)
178 string result = word;
180 if (!uncountables.Contains(word.ToLower()))
182 for (int i = rules.Count - 1; i >= 0; i--)
184 Rule rule = (Rule)rules[i];
186 if ((result = rule.Apply(word)) != null)
188 break;
193 return result;