Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NamingHelper.cs
Go to the documentation of this file.
1 // Copyright (c) 2014 Silicon Studio Corp. (http://siliconstudio.co.jp)
2 // This file is distributed under GPL v3. See LICENSE.md for details.
3 
4 using System;
5 using System.Linq;
6 using System.Text.RegularExpressions;
7 
8 namespace SiliconStudio.Core
9 {
10  /// <summary>
11  /// Helper to verify naming conventions.
12  /// </summary>
13  public static class NamingHelper
14  {
15  private static readonly Regex MatchIdentifier = new Regex("^[a-zA-Z_][a-zA-Z0-9_]*$");
16 
17  /// <summary>
18  /// Determines whether the specified string is valid namespace identifier.
19  /// </summary>
20  /// <param name="text">The namespace text.</param>
21  /// <returns><c>true</c> if is a valid namespace identifier; otherwise, <c>false</c>.</returns>
22  public static bool IsValidNamespace(string text)
23  {
24  string error;
25  return IsValidNamespace(text, out error);
26  }
27 
28  /// <summary>
29  /// Determines whether the specified string is valid namespace identifier.
30  /// </summary>
31  /// <param name="text">The namespace text.</param>
32  /// <param name="error">The error if return is false.</param>
33  /// <returns><c>true</c> if is a valid namespace identifier; otherwise, <c>false</c>.</returns>
34  public static bool IsValidNamespace(string text, out string error)
35  {
36  if (text == null) throw new ArgumentNullException("text");
37 
38  if (string.IsNullOrWhiteSpace(text))
39  {
40  error = "Namespace cannot be empty";
41  }
42  else
43  {
44  var items = text.Split(new[] { '.' }, StringSplitOptions.None);
45  error = items.Where(s => !IsIdentifier(s)).Select(item => string.Format("[{0}]", item, text)).FirstOrDefault();
46  }
47  return error == null;
48  }
49 
50  /// <summary>
51  /// Determines whether the specified text is a C# identifier.
52  /// </summary>
53  /// <param name="text">The text.</param>
54  /// <returns><c>true</c> if the specified text is an identifier; otherwise, <c>false</c>.</returns>
55  /// <exception cref="System.ArgumentNullException">text</exception>
56  public static bool IsIdentifier(string text)
57  {
58  if (text == null) throw new ArgumentNullException("text");
59  return MatchIdentifier.Match(text).Success;
60  }
61  }
62 }
function s(a)
static bool IsValidNamespace(string text, out string error)
Determines whether the specified string is valid namespace identifier.
Definition: NamingHelper.cs:34
static bool IsIdentifier(string text)
Determines whether the specified text is a C# identifier.
Definition: NamingHelper.cs:56
Helper to verify naming conventions.
Definition: NamingHelper.cs:13
static bool IsValidNamespace(string text)
Determines whether the specified string is valid namespace identifier.
Definition: NamingHelper.cs:22