private static bool IsValidManifestEscapeSequence(int c, ISource source, ref string validText, ref bool isEscapeSequence, ref bool isUnicodeSyntax, ref string unicodeCharacter, out IErrorStringValidity error) { error = null; if (c == 'u' || c == 'U') { isEscapeSequence = false; isUnicodeSyntax = true; unicodeCharacter = string.Empty; } else { isEscapeSequence = false; bool IsTranslated = false; foreach (KeyValuePair <char, string> Entry in CSharpEscapeTable) { if (c == Entry.Value[1]) { IsTranslated = true; validText += Entry.Key; break; } } if (!IsTranslated) { error = new ErrorIllFormedString(source); return(false); } } return(true); }
private static bool IsValidUnicodeSyntax(int c, ISource source, ref string validText, ref bool isUnicodeSyntax, ref string unicodeCharacter, out IErrorStringValidity error) { error = null; if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F')) { unicodeCharacter += (char)c; if (unicodeCharacter.Length == 4) { bool IsTranslated = int.TryParse(unicodeCharacter, NumberStyles.HexNumber, null, out int CodePoint); Debug.Assert(IsTranslated); byte[] CodeBytes = BitConverter.GetBytes(CodePoint); validText += Encoding.UTF32.GetString(CodeBytes); isUnicodeSyntax = false; unicodeCharacter = string.Empty; } } else { error = new ErrorIllFormedString(source); return(false); } return(true); }
/// <summary> /// Checks if a manifest string is valid. /// </summary> /// <param name="source">Location to use when reporting errors.</param> /// <param name="text">The text to check.</param> /// <param name="validText">If valid, the normalized string to use instead of <paramref name="text"/>.</param> /// <param name="error">If not valid, the error to report.</param> /// <returns>True if the manifest string is valid.</returns> public static bool IsValidManifestString(ISource source, string text, out string validText, out IErrorStringValidity error) { validText = string.Empty; error = null; bool IsNormalized; try { IsNormalized = text.IsNormalized(NormalizationForm.FormD); if (!IsNormalized) { throw new ArgumentException(); } } catch { IsNormalized = false; } if (!IsNormalized) { error = new ErrorIllFormedString(source); return(false); } byte[] Bytes = Encoding.UTF32.GetBytes(text); int[] Codes = new int[Bytes.Length / 4]; for (int i = 0; i < Codes.Length; i++) { Codes[i] = BitConverter.ToInt32(Bytes, i * 4); } bool IsEscapeSequence = false; bool IsUnicodeSyntax = false; string UnicodeCharacter = string.Empty; for (int i = 0; i < Codes.Length; i++) { if (!IsValidManifestCharacter(Bytes, Codes, source, text, ref validText, i, ref IsEscapeSequence, ref IsUnicodeSyntax, ref UnicodeCharacter, out error)) { return(false); } } if (IsUnicodeSyntax || IsEscapeSequence) { error = new ErrorIllFormedString(source); return(false); } Debug.Assert(validText.IsNormalized(NormalizationForm.FormD)); return(true); }
/// <summary> /// Checks if an identifier or name is valid. /// </summary> /// <param name="source">Location to use when reporting errors.</param> /// <param name="text">The text to check.</param> /// <param name="validText">If valid, the normalized string to use instead of <paramref name="text"/>.</param> /// <param name="error">If not valid, the error to report.</param> /// <returns>True if the identifier or name is valid.</returns> public static bool IsValidIdentifier(ISource source, string text, out string validText, out IErrorStringValidity error) { validText = string.Empty; error = null; bool IsNormalized; try { IsNormalized = text.IsNormalized(NormalizationForm.FormD); if (!IsNormalized) { throw new ArgumentException(); } } catch { IsNormalized = false; } if (!IsNormalized) { error = new ErrorIllFormedString(source); return(false); } bool IsWhiteSpaceFound = false; byte[] Bytes = Encoding.UTF32.GetBytes(text); int[] Codes = new int[Bytes.Length / 4]; for (int i = 0; i < Codes.Length; i++) { Codes[i] = BitConverter.ToInt32(Bytes, i * 4); } for (int i = 0; i < Codes.Length; i++) { if (!IsValidIdentifierCharacter(Codes, text, i, ref IsWhiteSpaceFound, ref validText, source, out error)) { return(false); } } if (validText.Length == 0) { error = new ErrorEmptyString(source); return(false); } Debug.Assert(validText.IsNormalized(NormalizationForm.FormD)); return(true); }