This class provides usefull text methods.
Beispiel #1
0
        /// <summary>
        /// Reads word from string. Returns null if no word is available.
        /// Word reading begins from first char, for example if SP"text", then space is trimmed.
        /// </summary>
        /// <param name="unQuote">Specifies if quoted string word is unquoted.</param>
        /// <param name="wordTerminatorChars">Specifies chars what terminate word.</param>
        /// <param name="removeWordTerminator">Specifies if work terminator is removed.</param>
        /// <returns></returns>
        public string ReadWord(bool unQuote, char[] wordTerminatorChars, bool removeWordTerminator)
        {
            // Always start word reading from first char.
            this.ReadToFirstChar();

            if (this.Available == 0)
            {
                return(null);
            }

            // quoted word can contain any char, " must be escaped with \
            // unqouted word can conatin any char except: SP VTAB HTAB,{}()[]<>

            if (m_SourceString.StartsWith("\""))
            {
                if (unQuote)
                {
                    return(TextUtils.UnQuoteString(QuotedReadToDelimiter(wordTerminatorChars, removeWordTerminator)));
                }
                else
                {
                    return(QuotedReadToDelimiter(wordTerminatorChars, removeWordTerminator));
                }
            }
            else
            {
                int wordLength = 0;
                for (int i = 0; i < m_SourceString.Length; i++)
                {
                    char c = m_SourceString[i];

                    bool isTerminator = false;
                    foreach (char terminator in wordTerminatorChars)
                    {
                        if (c == terminator)
                        {
                            isTerminator = true;
                            break;
                        }
                    }
                    if (isTerminator)
                    {
                        break;
                    }

                    wordLength++;
                }

                string retVal = m_SourceString.Substring(0, wordLength);
                if (removeWordTerminator)
                {
                    if (m_SourceString.Length >= wordLength + 1)
                    {
                        m_SourceString = m_SourceString.Substring(wordLength + 1);
                    }
                }
                else
                {
                    m_SourceString = m_SourceString.Substring(wordLength);
                }

                return(retVal);
            }
        }
        /// <summary>
        /// Parses SIP_Uri from SIP-URI string.
        /// </summary>
        /// <param name="value">SIP-URI  string.</param>
        /// <returns>Returns parsed SIP_Uri object.</returns>
        /// <exception cref="ArgumentNullException">Raised when <b>reader</b> is null.</exception>
        /// <exception cref="SIP_ParseException">Raised when invalid SIP message.</exception>
        protected override void ParseInternal(string value)
        {
            // Syntax: sip:/sips: username@host:port *[;parameter] [?header *[&header]]

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            value = Uri.UnescapeDataString(value);

            if (!(value.ToLower().StartsWith("sip:") || value.ToLower().StartsWith("sips:")))
            {
                throw new SIP_ParseException("Specified value is invalid SIP-URI !");
            }

            StringReader r = new StringReader(value);

            // IsSecure
            this.IsSecure = r.QuotedReadToDelimiter(':').ToLower() == "sips";

            // Get username
            if (r.SourceString.IndexOf('@') > -1)
            {
                this.User = r.QuotedReadToDelimiter('@');
            }

            // Gets host[:port]
            string[] host_port = r.QuotedReadToDelimiter(new char[] { ';', '?' }, false).Split(':');
            this.Host = host_port[0];
            // Optional port specified
            if (host_port.Length == 2)
            {
                this.Port = Convert.ToInt32(host_port[1]);
            }

            // We have parameters and/or header
            if (r.Available > 0)
            {
                // Get parameters
                string[] parameters = TextUtils.SplitQuotedString(r.QuotedReadToDelimiter('?'), ';');
                foreach (string parameter in parameters)
                {
                    if (parameter.Trim() != "")
                    {
                        string[] name_value = parameter.Trim().Split(new char[] { '=' }, 2);
                        if (name_value.Length == 2)
                        {
                            this.Parameters.Add(name_value[0], TextUtils.UnQuoteString(name_value[1]));
                        }
                        else
                        {
                            this.Parameters.Add(name_value[0], null);
                        }
                    }
                }

                // We have header
                if (r.Available > 0)
                {
                    this.m_Header = r.ReadToEnd();
                }
            }
        }