예제 #1
0
        /// <summary>
        /// Encodes the specified <see cref="System.String"/>.
        /// </summary>
        /// <param name="s">The string to encode.</param>
        /// <returns>The encoded string.</returns>
        public static string Encode(string s)
        {
            if (s == null)
            {
                throw new ArgumentNullException("s");
            }

            if (s.Equals(string.Empty) || !JsString.ShouldEncode(s))
            {
                return(string.Concat("\"", s, "\""));
            }

            char          ch;
            StringBuilder sb = new StringBuilder(s.Length);

            sb.Append('"');
            for (int i = 0; i < s.Length; ++i)
            {
                ch = s[i];
                switch (ch)
                {
                case '"':
                    sb.Append(@"\""");
                    break;

                case '/':
                    sb.Append(@"\/");
                    break;

                case '\\':
                    sb.Append(@"\\");
                    break;

                case '\b':
                    sb.Append(@"\b");
                    break;

                case '\f':
                    sb.Append(@"\f");
                    break;

                case '\n':
                    sb.Append(@"\n");
                    break;

                case '\r':
                    sb.Append(@"\r");
                    break;

                case '\t':
                    sb.Append(@"\t");
                    break;

                default:
                    if (ch > 0x7F)
                    {
                        // TODO: MUST add support for UTF-16.
                        sb.AppendFormat(@"\u{0}", ((int)ch).ToString("X4"));
                    }
                    else
                    {
                        sb.Append(ch);
                    }
                    break;
                }
            }
            sb.Append('"');

            return(sb.ToString());
        }
예제 #2
0
 /// <summary>
 /// Returns a value indicating equality with the specified instance.
 /// </summary>
 /// <param name="other">The JsNumber to compare.</param>
 /// <returns></returns>
 public int CompareTo(JsString other)
 {
     return(other != null?this.Value.CompareTo(other.Value) : -1);
 }
예제 #3
0
 /// <summary>
 /// Inequality operator.
 /// </summary>
 /// <param name="a">The first JsString.</param>
 /// <param name="b">The second JsString.</param>
 /// <returns>True if the JsStrings are not equal, otherwise; false.</returns>
 public static bool operator !=(JsString a, JsString b)
 {
     return(!JsString.Equals(a, b));
 }
예제 #4
0
 /// <summary>
 /// Returns a indicating whether this instance is equal to the specified
 /// JsString.
 /// </summary>
 /// <param name="other">The value to compare.</param>
 /// <returns>True if the specified instance is equal to this instance, otherwise;
 /// false.</returns>
 public bool Equals(JsString other)
 {
     return(other != null && Equals(other.Value));
 }