|
| 1 | +// ------------------------------------------------------------------------------------------------- |
| 2 | +// <copyright file="StringExtensions.cs" company="Starion Group S.A."> |
| 3 | +// |
| 4 | +// Copyright 2022-2026 Starion Group S.A. |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// </copyright> |
| 19 | +// ------------------------------------------------------------------------------------------------ |
| 20 | + |
| 21 | +namespace SysML2.NET.CodeGenerator.Extensions |
| 22 | +{ |
| 23 | + using System.Text; |
| 24 | + |
| 25 | + /// <summary> |
| 26 | + /// String helpers used by the grammar and lexical-rules code-generation passes. |
| 27 | + /// </summary> |
| 28 | + public static class StringExtensions |
| 29 | + { |
| 30 | + /// <summary> |
| 31 | + /// Tests whether a string follows the SysML lexical-rule naming convention: uppercase |
| 32 | + /// letters, digits and underscores only, with at least one letter (e.g. |
| 33 | + /// <c>RESERVED_KEYWORD</c>, <c>DEFINED_BY</c>, <c>STRING_VALUE</c>). |
| 34 | + /// </summary> |
| 35 | + /// <param name="value">The string to test</param> |
| 36 | + /// <returns><c>true</c> when the string matches the all-uppercase snake-case convention</returns> |
| 37 | + public static bool IsAllUpperSnake(this string value) |
| 38 | + { |
| 39 | + if (string.IsNullOrEmpty(value)) |
| 40 | + { |
| 41 | + return false; |
| 42 | + } |
| 43 | + |
| 44 | + var hasLetter = false; |
| 45 | + |
| 46 | + foreach (var character in value) |
| 47 | + { |
| 48 | + if (char.IsLetter(character)) |
| 49 | + { |
| 50 | + hasLetter = true; |
| 51 | + |
| 52 | + if (!char.IsUpper(character)) |
| 53 | + { |
| 54 | + return false; |
| 55 | + } |
| 56 | + } |
| 57 | + else if (character != '_' && !char.IsDigit(character)) |
| 58 | + { |
| 59 | + return false; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return hasLetter; |
| 64 | + } |
| 65 | + |
| 66 | + /// <summary> |
| 67 | + /// Tests whether the supplied string contains any letter. |
| 68 | + /// </summary> |
| 69 | + /// <param name="value">The string to test</param> |
| 70 | + /// <returns><c>true</c> when at least one letter is present</returns> |
| 71 | + public static bool ContainsAnyLetter(this string value) |
| 72 | + { |
| 73 | + if (string.IsNullOrEmpty(value)) |
| 74 | + { |
| 75 | + return false; |
| 76 | + } |
| 77 | + |
| 78 | + foreach (var character in value) |
| 79 | + { |
| 80 | + if (char.IsLetter(character)) |
| 81 | + { |
| 82 | + return true; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + /// <summary> |
| 90 | + /// Converts a <c>SNAKE_CASE</c> identifier into <c>PascalCase</c> |
| 91 | + /// (e.g. <c>DEFINED_BY</c> → <c>DefinedBy</c>). |
| 92 | + /// </summary> |
| 93 | + /// <param name="value">The snake-case identifier</param> |
| 94 | + /// <returns>The PascalCase form</returns> |
| 95 | + public static string ToPascalCaseFromSnake(this string value) |
| 96 | + { |
| 97 | + if (string.IsNullOrEmpty(value)) |
| 98 | + { |
| 99 | + return value; |
| 100 | + } |
| 101 | + |
| 102 | + var builder = new StringBuilder(value.Length); |
| 103 | + var capitalizeNext = true; |
| 104 | + |
| 105 | + foreach (var character in value) |
| 106 | + { |
| 107 | + if (character == '_') |
| 108 | + { |
| 109 | + capitalizeNext = true; |
| 110 | + continue; |
| 111 | + } |
| 112 | + |
| 113 | + builder.Append(capitalizeNext ? char.ToUpperInvariant(character) : char.ToLowerInvariant(character)); |
| 114 | + capitalizeNext = false; |
| 115 | + } |
| 116 | + |
| 117 | + return builder.ToString(); |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments