Esempio n. 1
0
        /// <summary>
        /// 格式化Cookie名称
        /// </summary>
        /// <param name="name">Cookie名称</param>
        /// <returns>格式化后Cookie名称</returns>
        public unsafe static string FormatCookieName(string name)
        {
            if (string.IsNullOrEmpty(name))
            {
                log.Error.Throw(log.exceptionType.Null);
            }
            if (name.Length > MaxCookieNameLength)
                log.Error.Throw(null, "cookie名称超过限定 " + ((uint)name.Length).toString(), false);
            fixed(char *nameFixed = name)
            {
                char *endName = nameFixed + name.Length;
                int   count   = unsafer.String.AsciiCount(nameFixed, endName, formatCookieNameCharMap.Map);

                if (*nameFixed == '$')
                {
                    ++count;
                }
                if (count != 0)
                {
                    string newName = fastCSharp.String.FastAllocateString(count = name.Length + (count << 1));
                    fixed(char *newNameFixed = newName)
                    {
                        char *nextCookieName = newNameFixed;

                        if (*nameFixed == '$')
                        {
                            nextCookieName       += 2;
                            *(uint *)newNameFixed = '%' + ('2' << 16);
                            *nextCookieName = '4';
                        }
                        else
                        {
                            *nextCookieName = *nameFixed;
                        }
                        char *nextName = nameFixed;

                        String.asciiMap formatMap = formatCookieNameCharMap;
                        while (++nextName != endName)
                        {
                            uint nextValue = (uint)*nextName;
                            if (formatMap.Get((int)nextValue))
                            {
                                *++nextCookieName = '%';
                                *++nextCookieName = (char)numberExpand.ToHex(nextValue >> 4);
                                *++nextCookieName = (char)numberExpand.ToHex(nextValue & 15);
                            }
                            else
                            {
                                *++nextCookieName = (char)nextValue;
                            }
                        }
                        return(newName);
                    }
                }
            }

            return(name);
        }
Esempio n. 2
0
        /// <summary>
        /// 格式化Cookie名称
        /// </summary>
        /// <param name="name">Cookie名称</param>
        /// <returns>格式化后Cookie名称</returns>
        public unsafe static string FormatCookieName(string name)
        {
            if (name.length() == 0)
            {
                log.Default.Throw(log.exceptionType.Null);
            }
            if (name.Length > MaxCookieNameLength)
                log.Default.Throw(null, "cookie名称超过限定 " + ((uint)name.Length).toString(), false);
            fixed(char *nameFixed = name)
            {
                char *endName = nameFixed + name.Length;
                int   count   = unsafer.String.asciiCount(nameFixed, endName, formatCookieNameCharMap.Map, FormatCookieNameChars[0]);

                if (*nameFixed == '$')
                {
                    ++count;
                }
                if (count != 0)
                {
                    char *nameChars = stackalloc char[count = name.Length + (count << 1)], nextCookieName = nameChars;
                    if (*nameFixed == '$')
                    {
                        *nextCookieName++ = '%';
                        *(uint *)nextCookieName = '2' + ('4' << 16);
                        nextCookieName         += 2;
                    }
                    else
                    {
                        *nextCookieName = *nameFixed;
                    }
                    char *          nextName  = nameFixed;
                    String.asciiMap formatMap = formatCookieNameCharMap;
                    while (++nextName != endName)
                    {
                        int nextValue = (int)*nextName;
                        if (formatMap.Get(nextValue))
                        {
                            *++nextCookieName = '%';
                            int highValue = nextValue >> 4;
                            *++nextCookieName = (char)(highValue < 10 ? highValue + '0' : (highValue + ('0' + 'A' - '9' - 1)));
                            nextValue        &= 15;
                            *++nextCookieName = (char)(nextValue < 10 ? nextValue + '0' : (nextValue + ('0' + 'A' - '9' - 1)));
                        }
                        else
                        {
                            *++nextCookieName = (char)nextValue;
                        }
                    }
                    return(new string(nameChars, 0, count));
                }
            }

            return(name);
        }