NormalizeNFKC() public static method

Applies NFKC normalization to a string.
public static NormalizeNFKC ( string sbIn ) : string
sbIn string
return string
Exemplo n.º 1
0
        /// <summary>
        /// Preps a name according to the Stringprep profile defined in
        ///     RFC3491.
        ///     *
        /// </summary>
        /// <param name="input">
        /// the name to prep.
        /// </param>
        /// <param name="allowUnassigned">
        /// true if the name may contain unassigned
        ///     code points.
        /// </param>
        /// <returns>
        /// the prepped name.
        ///     @throws StringprepException If the name cannot be prepped with
        ///     this profile.
        ///     @throws NullPointerException If the name is null.
        /// </returns>
        public static string NamePrep(string input, bool allowUnassigned)
        {
            if (input == null)
            {
                throw new NullReferenceException();
            }

            var s = new StringBuilder(input);

            if (!allowUnassigned && Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.ContainsUnassigned);
            }

            Filter(s, RFC3454.B1);
            Map(s, RFC3454.B2search, RFC3454.B2replace);

            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));

// B.3 is only needed if NFKC is not used, right?
            // map(s, RFC3454.B3search, RFC3454.B3replace);
            if (Contains(s, RFC3454.C12) || Contains(s, RFC3454.C22) || Contains(s, RFC3454.C3) ||
                Contains(s, RFC3454.C4) || Contains(s, RFC3454.C5) || Contains(s, RFC3454.C6) || Contains(s, RFC3454.C7) ||
                Contains(s, RFC3454.C8))
            {
                // Table C.9 only contains code points > 0xFFFF which Java
                // doesn't handle
                throw new StringprepException(StringprepException.ContainsProhibited);
            }

            // Bidi handling
            bool r = Contains(s, RFC3454.D1);
            bool l = Contains(s, RFC3454.D2);

            // RFC 3454, section 6, requirement 1: already handled above (table C.8)

            // RFC 3454, section 6, requirement 2
            if (r && l)
            {
                throw new StringprepException(StringprepException.BidiBothral);
            }

            // RFC 3454, section 6, requirement 3
            if (r)
            {
                if (!Contains(s[0], RFC3454.D1) || !Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BidiLtral);
                }
            }

            return(s.ToString());
        }
Exemplo n.º 2
0
        /// <summary>
        /// Preps a node name according to the Stringprep profile defined in RFC3920.
        /// </summary>
        /// <param name="input">the node name to prep.
        /// </param>
        /// <param name="allowUnassigned">true if the node name may contain
        /// unassigned code points.
        /// </param>
        /// <returns> the prepped node name.
        /// @throws StringprepException If the node name cannot be prepped
        /// with this profile.
        /// @throws NullPointerException If the node name is null.
        ///
        /// </returns>
        public static string NodePrep(string input, bool allowUnassigned)
        {
            if (input == null)
            {
                throw new System.NullReferenceException();
            }

            StringBuilder s = new StringBuilder(input);

            if (!allowUnassigned && Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
            }

            Filter(s, RFC3454.B1);
            Map(s, RFC3454.B2search, RFC3454.B2replace);

            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));

            if (Contains(s, RFC3454.C11) || Contains(s, RFC3454.C12) || Contains(s, RFC3454.C21) || Contains(s, RFC3454.C22) || Contains(s, RFC3454.C3) || Contains(s, RFC3454.C4) || Contains(s, RFC3454.C5) || Contains(s, RFC3454.C6) || Contains(s, RFC3454.C7) || Contains(s, RFC3454.C8) || Contains(s, RFC3920_NODEPREP_PROHIBIT))
            {
                // Table C.9 only contains code points > 0xFFFF which Java
                // doesn't handle
                throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
            }

            // Bidi handling
            bool r = Contains(s, RFC3454.D1);
            bool l = Contains(s, RFC3454.D2);

            // RFC 3454, section 6, requirement 1: already handled above (table C.8)

            // RFC 3454, section 6, requirement 2
            if (r && l)
            {
                throw new StringprepException(StringprepException.BIDI_BOTHRAL);
            }

            // RFC 3454, section 6, requirement 3
            if (r)
            {
                if (!Contains(s[0], RFC3454.D1) || !Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BIDI_LTRAL);
                }
            }

            return(s.ToString());
        }
Exemplo n.º 3
0
        /// <summary>
        /// The sasl prep.
        /// </summary>
        /// <param name="input">
        /// The input.
        /// </param>
        /// <param name="allowUnsigned">
        /// The allow unsigned.
        /// </param>
        /// <returns>
        /// The <see cref="string"/>.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// </exception>
        /// <exception cref="StringprepException">
        /// </exception>
        public static string SASLPrep(string input, bool allowUnsigned = false)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            var s = new StringBuilder(input);

            if (!allowUnsigned && Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.ContainsUnassigned);
            }

            Filter(s, RFC3454.B1);
            Map(s, RFC3454.C12, SASLPREP_SPACE_MAP);

            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));

            if (Contains(s, RFC3454.C12) || Contains(s, RFC3454.C21) || Contains(s, RFC3454.C22) ||
                Contains(s, RFC3454.C3) || Contains(s, RFC3454.C4) || Contains(s, RFC3454.C5) || Contains(s, RFC3454.C6) ||
                Contains(s, RFC3454.C7) || Contains(s, RFC3454.C8))
            {
                throw new StringprepException(StringprepException.ContainsProhibited);
            }

            bool r = Contains(s, RFC3454.D1);
            bool l = Contains(s, RFC3454.D2);

            if (r && l)
            {
                throw new StringprepException(StringprepException.BidiBothral);
            }

            if (r)
            {
                if (!Contains(s[0], RFC3454.D1) || !Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BidiLtral);
                }
            }

            return(s.ToString());
        }
Exemplo n.º 4
0
        public static string SASLPrep(string input, bool allowUnsigned = false)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            StringBuilder s = new StringBuilder(input);

            if (!allowUnsigned && Contains(s, RFC3454.A1))
            {
                throw new StringprepException(StringprepException.CONTAINS_UNASSIGNED);
            }

            Filter(s, RFC3454.B1);
            Map(s, RFC3454.C12, SASLPREP_SPACE_MAP);

            s = new StringBuilder(NFKC.NormalizeNFKC(s.ToString()));

            if (Contains(s, RFC3454.C12) || Contains(s, RFC3454.C21) || Contains(s, RFC3454.C22) || Contains(s, RFC3454.C3) || Contains(s, RFC3454.C4) || Contains(s, RFC3454.C5) || Contains(s, RFC3454.C6) || Contains(s, RFC3454.C7) || Contains(s, RFC3454.C8))
            {
                throw new StringprepException(StringprepException.CONTAINS_PROHIBITED);
            }

            bool r = Contains(s, RFC3454.D1);
            bool l = Contains(s, RFC3454.D2);

            if (r && l)
            {
                throw new StringprepException(StringprepException.BIDI_BOTHRAL);
            }

            if (r)
            {
                if (!Contains(s[0], RFC3454.D1) || !Contains(s[s.Length - 1], RFC3454.D1))
                {
                    throw new StringprepException(StringprepException.BIDI_LTRAL);
                }
            }

            return(s.ToString());
        }