Exemplo n.º 1
0
        static unsafe void StringCtor(byte *str, sbyte *value)
        {
            int   len = strlen((byte *)value);
            void *dst = str + StringOperations.GetDataOffset();

            mbstowcs((char *)dst, (byte *)value, len);
        }
Exemplo n.º 2
0
        static unsafe bool InternalStrcpy(byte *dest, int destPos, byte *src, int srcPos, int count)
        {
            /* Get size of src and dest */
            int srcLength  = *(int *)(src + StringOperations.GetLengthOffset());
            int destLength = *(int *)(dest + StringOperations.GetLengthOffset());

            /* Ensure the source and destination are big enough */
            if (destPos < 0)
            {
                return(false);
            }
            if (srcPos < 0)
            {
                return(false);
            }
            if (count < 0)
            {
                return(false);
            }
            if (destPos + count > destLength)
            {
                return(false);
            }
            if (srcPos + count > srcLength)
            {
                return(false);
            }

            /* Do the copy */
            MemoryOperations.MemCpy((void *)(dest + StringOperations.GetDataOffset() + destPos * 2),
                                    (void *)(src + StringOperations.GetDataOffset() + srcPos * 2), count * 2);

            return(true);
        }
Exemplo n.º 3
0
        static unsafe void StringCtor(byte *str, char *value, int startIndex, int length)
        {
            void *src = value + startIndex;
            int   len = length * sizeof(char);
            void *dst = str + StringOperations.GetDataOffset();

            MemoryOperations.MemCpy(dst, src, len);
        }
Exemplo n.º 4
0
        static unsafe void StringCtor(byte *str, char[] srcArr, int startIndex, int length)
        {
            void *src = (byte *)MemoryOperations.GetInternalArray(srcArr) + sizeof(char) * startIndex;
            int   len = length * sizeof(char);
            void *dst = str + StringOperations.GetDataOffset();

            MemoryOperations.MemCpy(dst, src, len);
        }
Exemplo n.º 5
0
        static unsafe void StringCtor(byte *str, char[] srcArr)
        {
            void *src = MemoryOperations.GetInternalArray(srcArr);
            int   len = srcArr.Length * sizeof(char);
            void *dst = str + StringOperations.GetDataOffset();

            MemoryOperations.MemCpy(dst, src, len);
        }
Exemplo n.º 6
0
        static unsafe void StringCtor(byte *str, char c, int count)
        {
            char *dst = (char *)(str + StringOperations.GetDataOffset());

            for (int i = 0; i < count; i++)
            {
                *dst++ = c;
            }
        }
Exemplo n.º 7
0
        static unsafe int CompareOrdinalHelper(byte *strA, int indexA, int countA, byte *strB, int indexB, int countB)
        {
            char *a = (char *)(strA + StringOperations.GetDataOffset()) + indexA;
            char *b = (char *)(strB + StringOperations.GetDataOffset()) + indexB;

            for (int i = 0; i < countA; i++, a++, b++)
            {
                if (*a < *b)
                {
                    return(-1);
                }
                else if (*a > *b)
                {
                    return(1);
                }
            }

            return(0);
        }
Exemplo n.º 8
0
        /* The main format function */
        static string FormatInteger(byte *v, bool is_signed, int blen, string fmt, NumberFormatInfo nfi)
        {
            long  s  = *(long *)v;
            ulong us = *(ulong *)v;

            bool is_negative = false;

            if (is_signed)
            {
                if (s < 0)
                {
                    is_negative = true;
                    us          = (ulong)(-s);
                }
                else
                {
                    us = (ulong)s;
                }
            }

            const int MAX_STR = 256;

            char *ret     = stackalloc char[MAX_STR];
            int   cur_ret = 0;

            if (fmt == null || fmt.Equals(string.Empty))
            {
                fmt = "G";
            }
            char *f       = StringOperations.GetChars(fmt);
            int   cur_fmt = 0;
            int   l_fmt   = fmt.Length;

            char c_f = *f;      // Current formatting character
            int  p   = -1;      // Current precision (used if 'G' is converted to 'F')

            while (cur_fmt < l_fmt)
            {
                switch (c_f)
                {
                case 'G':
                case 'g':
                {
                    /* Generic format strings are either fixed point or exponential, depending on
                     * the exponent of the number */
                    int sig_digits = get_number_from_fmt_string(f, cur_fmt, l_fmt, out int new_cur_fmt);
                    if (sig_digits == -1)
                    {
                        switch (blen)
                        {
                        case 4:
                            sig_digits = 10;
                            break;

                        case 8:
                            sig_digits = 19;
                            break;

                        default:
                            sig_digits = 29;
                            break;
                        }
                    }
                    int exp = get_exponent_u(us);
                    if (exp < sig_digits && exp >= -4)
                    {
                        c_f = 'F';
                        p   = 0;
                    }
                    else
                    {
                        if (c_f == 'G')
                        {
                            c_f = 'E';
                        }
                        else
                        {
                            c_f = 'e';
                        }
                    }
                }
                    continue;

                case 'F':
                case 'f':
                case 'N':
                case 'n':
                {
                    if (p == -1)
                    {
                        p = get_number_from_fmt_string(f, cur_fmt + 1, l_fmt, out cur_fmt);
                    }
                    else
                    {
                        get_number_from_fmt_string(f, cur_fmt + 1, l_fmt, out cur_fmt);             // ignore p in the string (this is a 'G' converted to 'F')
                    }
                    if (p == -1)
                    {
                        if (nfi != null)
                        {
                            p = nfi.NumberDecimalDigits;
                        }
                        else
                        {
                            p = 2;
                        }
                    }
                    if (is_negative)
                    {
                        if (nfi != null && nfi.NegativeSign != null)
                        {
                            append_string(ret, nfi.NegativeSign, ref cur_ret, MAX_STR);
                        }
                        else
                        {
                            append_string(ret, "-", ref cur_ret, MAX_STR);
                        }
                    }

                    /* build a string before the decimal point */
                    char *   rev_str     = stackalloc char[MAX_STR];
                    int      cur_rev_str = 0;
                    ulong    c_us        = us;
                    string[] digits      = null;
                    if (nfi != null && nfi.NativeDigits != null)
                    {
                        digits = nfi.NativeDigits;
                    }
                    while (c_us != 0)
                    {
                        if (digits == null)
                        {
                            rev_str[cur_rev_str++] = (char)('0' + (c_us % 10));
                        }
                        else
                        {
                            append_string(rev_str, digits[(int)(c_us % 10)], ref cur_rev_str, MAX_STR);
                        }
                        c_us /= 10;
                    }

                    /* append back onto the original string in reverse order */
                    if (cur_rev_str == 0)
                    {
                        append_string(ret, "0", ref cur_ret, MAX_STR);
                    }
                    else
                    {
                        while (cur_rev_str > 0)
                        {
                            char c = rev_str[--cur_rev_str];
                            if (((c_f == 'n') || (c_f == 'N')) && cur_rev_str != 0 && ((cur_rev_str % 3) == 0))
                            {
                                if (nfi != null && nfi.NumberGroupSeparator != null)
                                {
                                    append_string(ret, nfi.NumberGroupSeparator, ref cur_ret, MAX_STR);
                                }
                                else
                                {
                                    append_string(ret, ",", ref cur_ret, MAX_STR);
                                }
                            }
                            if (cur_ret < MAX_STR)
                            {
                                ret[cur_ret++] = c;
                            }
                        }
                    }

                    if (p > 0)
                    {
                        /* Add .0000... after string */
                        if (nfi != null && nfi.NumberDecimalSeparator != null)
                        {
                            append_string(ret, nfi.NumberDecimalSeparator, ref cur_ret, MAX_STR);
                        }
                        else
                        {
                            append_string(ret, ".", ref cur_ret, MAX_STR);
                        }

                        var cur_p = p;
                        while (p-- > 0)
                        {
                            append_string(ret, "0", ref cur_ret, MAX_STR);
                        }
                    }

                    p = -1;             // reset precision so that we look for it again in the next formatting operation - if 'G' then it will be set to zero again
                }
                break;

                case 'X':
                case 'x':
                {
                    int dcount = get_number_from_fmt_string(f, cur_fmt + 1, l_fmt, out cur_fmt);
                    if (dcount == -1)
                    {
                        dcount = blen * 2;
                    }
                    string digits;
                    if (c_f == 'X')
                    {
                        digits = uppercaseDigits;
                    }
                    else
                    {
                        digits = lowercaseDigits;
                    }
                    for (int i = dcount - 1; i >= 0; i--)
                    {
                        if (i > (blen * 2))
                        {
                            if (cur_ret < MAX_STR)
                            {
                                ret[cur_ret++] = '0';
                            }
                        }
                        else
                        {
                            byte b = v[i / 2];
                            if ((i % 2) == 1)
                            {
                                b >>= 4;
                            }
                            else
                            {
                                b &= 0xf;
                            }
                            if (cur_ret < MAX_STR)
                            {
                                ret[cur_ret++] = digits[b];
                            }
                        }
                    }
                }
                break;

                default:
                    /* Add verbatim */
                    if (cur_ret < MAX_STR)
                    {
                        ret[cur_ret++] = c_f;
                    }
                    cur_fmt++;
                    break;
                }
                c_f = f[cur_fmt];
            }

            return(new string(ret, 0, cur_ret));
        }