private static bool IsWhiteSpace(Char16 c)
 {
     return(c == UTF16CodeSet.code_space ||
            c == UTF16CodeSet.code_tab ||
            c == UTF16CodeSet.code_LF ||
            c == UTF16CodeSet.code_CR);
 }
Esempio n. 2
0
        /// <summary>
        /// strip target charactor from left and right side.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="alloc"></param>
        /// <returns></returns>
        public static NativeList <Char16> Strip(this NativeList <Char16> source, Char16 target, Allocator alloc)
        {
            var tmp = new NativeList <Char16>(alloc);

            Strip(source, target, tmp);
            return(tmp);
        }
        public static void Split(ReadOnlyStringEntity source, Char16 delim,
                                 NativeList <ReadOnlyStringEntity> result)
        {
            var tmp = result.GetUnsafeRef();

            SplitBurstCompile._splitCharDelegate(ref source, ref delim, ref tmp);
        }
Esempio n. 4
0
        /// <summary>
        /// strip target charactor from left and right side.
        /// </summary>
        /// <param name="source"></param>
        /// <param name="target"></param>
        /// <param name="result"></param>
        public unsafe static void Strip(this NativeList <Char16> source, Char16 target, NativeList <Char16> result)
        {
            result.Clear();
            var se = StripCharImpl(source.ToStringEntity(), target, true, true);

            result.AddRange((void *)se.GetUnsafePtr(), se.Length);
        }
Esempio n. 5
0
 private static WordType CheckCharType(Char16 c)
 {
     if (c.IsDigit(out int m))
     {
         return(WordType.Digit);
     }
     return(WordType.String);
 }
 public static ReadOnlyStringEntity Rstrip(ReadOnlyStringEntity source, Char16 target)
 {
     StripBurstCompile._stripCharDelegate(ref source,
                                          ref target,
                                          false, true,
                                          out ReadOnlyStringEntity result);
     return(result);
 }
        internal unsafe static void TryParseHex32Impl(Char16 *ptr_source,
                                                      int length,
                                                      out bool success,
                                                      out uint buf,
                                                      Endian endian)
        {
            const int n_digits = 8;  // accepts 8 digits set (4bit * 8)

            int i_start = 0;

            buf = 0;

            if (!(length == n_digits || length == n_digits + 2))
            {
                success = false;
                return;
            }

            if (ptr_source[0] == UTF16CodeSet.code_0 && ptr_source[1] == UTF16CodeSet.code_x)
            {
                i_start = 2;
            }

            if (endian == Endian.Big)
            {
                for (int i = i_start; i < length; i++)
                {
                    if (ptr_source[i].IsHex(out uint h))
                    {
                        buf = (buf << 4) | h;
                    }
                    else
                    {
                        success = false;
                        return;
                    }
                }
            }
            else if (endian == Endian.Little)
            {
                int n_byte = length / 2;
                int i_last = i_start / 2;
                for (int i = n_byte - 1; i >= i_last; i--)
                {
                    Char16 c0 = ptr_source[2 * i];
                    Char16 c1 = ptr_source[2 * i + 1];

                    if (c0.IsHex(out uint h0) && c1.IsHex(out uint h1))
                    {
                        buf = (buf << 4) | h0;
                        buf = (buf << 4) | h1;
                    }
                    else
                    {
                        success = false;
                        return;
                    }
                }
Esempio n. 8
0
        internal unsafe static T StripCharImpl <T>(T source, Char16 target, bool left, bool right)
            where T : IJaggedArraySliceBase <Char16>, ISlice <T>
        {
            // L side
            int start = 0;

            if (left)
            {
                for (int i = 0; i < source.Length; i++)
                {
                    if (source[i] != target)
                    {
                        start = i;
                        break;
                    }
                }
            }

            // R side
            int last = source.Length - 1;

            if (right)
            {
                for (int i = last; i >= start; i--)
                {
                    if (source[i] != target)
                    {
                        last = i;
                        break;
                    }
                }
            }

            // make result
            int len = last - start + 1;

            if (len > 0)
            {
                int end = last + 1;
                return((T)source.Slice(start, end));
            }
            else
            {
                return((T)source.Slice(0, 0));
            }
        }
Esempio n. 9
0
 /// <summary>
 /// match target is same as System.Char.IsWhiteSpace()
 /// </summary>
 /// <param name="c"></param>
 /// <returns></returns>
 public static bool IsWhiteSpace(this Char16 c)
 {
     if (c.Value == 0x20 ||
         c.Value == 0xA0 ||
         c.Value == 0x1680 ||
         (0x2000 <= c.Value && c.Value <= 0x200A) ||
         c.Value == 0x202F ||
         c.Value == 0x205F ||
         c.Value == 0x3000 ||
         c.Value == 0x2028 ||
         c.Value == 0x2029 ||
         c.Value == 0x0009 ||
         c.Value == 0x000A ||
         c.Value == 0x000B ||
         c.Value == 0x0085)
     {
         return(true);
     }
     else
     {
         return(false);
     }
 }
Esempio n. 10
0
                public int Compare(NativeJaggedArraySlice <WordBlock> left,
                                   NativeJaggedArraySlice <WordBlock> right)
                {
                    if (left.Length < 1 && right.Length < 1)
                    {
                        return(0);
                    }
                    if (left.Length < 1)
                    {
                        return(-1);
                    }
                    if (right.Length < 1)
                    {
                        return(1);
                    }

                    if (left[0].type != right[0].type)
                    {
                        if (left[0].type == WordType.Digit)
                        {
                            return(-1);
                        }
                        if (right[0].type == WordType.Digit)
                        {
                            return(1);
                        }
                        return(0);
                    }

                    int length = math.min(left.Length, right.Length);

                    for (int i = 0; i < length; i++)
                    {
                        var word_left  = left[i];
                        var word_right = right[i];
                        if (word_left.type == WordType.Digit)
                        {
                            if (word_left.num < word_right.num)
                            {
                                return(-1);
                            }
                            if (word_left.num > word_right.num)
                            {
                                return(1);
                            }
                        }
                        else if (word_left.type == WordType.String)
                        {
                            int word_len = math.min(word_left.word.Length, word_right.word.Length);
                            for (int j = 0; j < word_len; j++)
                            {
                                Char16 c_left  = word_left.word[j];
                                Char16 c_right = word_right.word[j];
                                if (c_left < c_right)
                                {
                                    return(-1);
                                }
                                if (c_left > c_right)
                                {
                                    return(1);
                                }
                            }
                            if (word_left.word.Length < word_right.word.Length)
                            {
                                return(-1);
                            }
                            if (word_left.word.Length > word_right.word.Length)
                            {
                                return(1);
                            }
                        }
                    }
                    if (left.Length < right.Length)
                    {
                        return(-1);
                    }
                    if (left.Length > right.Length)
                    {
                        return(1);
                    }
                    return(0);
                }
        internal static unsafe bool GetBytesImpl(Base64Info *info_ptr,
                                                 UnsafeRefToNativeList <byte> buff,
                                                 Char16 *char_ptr,
                                                 int char_len)
        {
            CheckLengthIsPositive(char_len);

            uint store   = info_ptr->store;
            int  bytePos = info_ptr->bytePos;

            for (int i = 0; i < char_len; i++)
            {
                Char16 c = char_ptr[i];
                if (IsWhiteSpace(c))
                {
                    continue;
                }

                if (c == '=')
                {
                    switch (bytePos)
                    {
                    case 0:
                    case 1:

                        /*
                         * var sb = new StringBuilder();
                         * sb.Append("bytePos = " + bytePos.ToString() + '\n');
                         * sb.Append("i = " + i.ToString() + "\n\n");
                         * sb.Append("decoded:\n");
                         * for(int j=0; j<buff.Length; j++)
                         * {
                         *  sb.Append(buff[j].ToString() + ' ');
                         * }
                         * sb.Append('\n');
                         * sb.Append("currect char: " + c + '\n');
                         * sb.Append("left char: " + c + '\n');
                         * int c_count = 0;
                         * for (int j=i+1; j<char_len; j++)
                         * {
                         *  sb.Append(char_ptr[j]);
                         *  c_count++;
                         *  if(c_count == 16)
                         *  {
                         *      sb.Append('\n');
                         *      c_count = 0;
                         *  }
                         * }
                         * sb.Append('\n');
                         * UnityEngine.Debug.Log(sb);
                         */

#if UNITY_EDITOR
                        throw new ArgumentException("invalid padding was detected.");
#else
                        return(false);
#endif
                    case 2:
                        // pick 1 byte from "**==" code
                        buff.Add((byte)((store & 0x0ff0) >> 4));
                        bytePos = 0;
                        break;

                    case 3:
                        // pick 2 byte from "***=" code
                        buff.Add((byte)((store & 0x03fc00) >> 10));
                        buff.Add((byte)((store & 0x0003fc) >> 2));
                        bytePos = 0;
                        break;
                    }
                    return(true);
                }
                else
                {
                    uint b = Base64DecodeTable.Get(c);
                    if (b != 255)
                    {
                        store = (store << 6) | (b & 0x3f);
                        bytePos++;
                    }
                    else
                    {
                        // invalid char for Base64
#if UNITY_EDITOR
                        throw new ArgumentException($"invalid code was detected. char={c}");
#else
                        return(false);
#endif
                    }
                }

                if (bytePos == 4)
                {
                    buff.Add((byte)((store & 0xff0000) >> 16));
                    buff.Add((byte)((store & 0x00ff00) >> 8));
                    buff.Add((byte)((store & 0x0000ff)));
                    store   = 0;
                    bytePos = 0;
                }
            }
            info_ptr->store   = store;
            info_ptr->bytePos = bytePos;

            return(true);
        }
        private unsafe static bool TryParseFloatFormat_v2(Char16 *ptr_source,
                                                          int length,
                                                          out int sign,
                                                          out int i_start,
                                                          out int dot_pos,
                                                          out int exp_pos,
                                                          out int n_pow)
        {
            // check sign on front
            i_start = 0;
            if (ptr_source[0].IsSign(out sign))
            {
                i_start = 1;
            }

            // eat zeros on the head
            bool is_zero_head = false;

            for (int i = i_start; i < length; i++)
            {
                UInt16 c = ptr_source[i];
                if (c == UTF16CodeSet.code_0)
                {
                    is_zero_head = true;
                    i_start++;
                }
                else
                {
                    break;
                }
            }

            // initialize value
            dot_pos = -1;
            exp_pos = -1;
            n_pow   = 0;

            if (length > 2048)
            {
                return(false);
            }

            // check dot pos
            // check exp pos
            int  dummy;
            bool is_digit_found = false;

            for (int i = i_start; i < length; i++)
            {
                Char16 c = ptr_source[i];

                if (!is_digit_found && c.IsDigit(out dummy))
                {
                    is_digit_found = true;
                }
                else if (c.IsDot())
                {
                    if (dot_pos >= 0)
                    {
                        return(false);               // # of dots must be 0 or 1 in string.
                    }
                    dot_pos = i;
                }
                else if (c.IsExp())
                {
                    if (!is_digit_found &&
                        !is_zero_head)
                    {
                        return(false);                // must have digits before [E/e] mark.
                    }
                    if (exp_pos > 0)
                    {
                        return(false);                // # of [E/e] marks must be 0 or 1 in string.
                    }
                    exp_pos = i;
                }
            }

            // check exp sign
            int exp_start = exp_pos + 1;
            int exp_sign  = 1;

            if (exp_pos > 0)
            {
                if (ptr_source[exp_start].IsSign(out exp_sign))
                {
                    exp_start += 1;
                }
                if (exp_start == length)
                {
                    return(false);                      // no digits in exp
                }
            }
            else
            {
                exp_pos   = (short)length;
                exp_start = exp_pos;
            }

            // check mantissa region is digits only or not
            for (int i = i_start; i < exp_pos; i++)
            {
                Char16 c = ptr_source[i];
                if (!c.IsDigit(out dummy) && !c.IsDot())
                {
                    return(false);
                }
            }

            // check exp region and extract n_pow
            if (exp_start > 0)
            {
                for (int i = 0; i < math.min(length - exp_start, 64); i++) // up to 64 digits for pow
                {
                    Char16 c = ptr_source[i + exp_start];

                    if (!c.IsDigit(out int d))
                    {
                        return(false);                        // exp region must be digits only
                    }
                    n_pow = n_pow * 10 + (int)d;
                    if (n_pow > 350)
                    {
                        if (exp_sign > 0)
                        {
                            // overflow
                            return(false);
                        }
                        break;
                    }
                }
                n_pow *= exp_sign;
            }

            // parsing manntissa search [i_start, exp_pos] and use dot_pos to calc 10^m in mantissa.
            if (dot_pos < 0)
            {
                dot_pos = exp_pos;
            }

            return(true);
        }
        private unsafe static bool TryParseFloatFormat(Char16 *ptr_source,
                                                       int length,
                                                       out int sign,
                                                       out int i_start,
                                                       out int dot_pos,
                                                       out int exp_pos,
                                                       out int n_pow)
        {
            i_start = 0;
            if (ptr_source[0].IsSign(out sign))
            {
                i_start = 1;
            }

            bool zero_head = false;

            // eat zeros on the head
            for (int i = i_start; i < length; i++)
            {
                UInt16 c = ptr_source[i];
                if (c == UTF16CodeSet.code_0)
                {
                    i_start++;
                    zero_head = true;
                }
                else
                {
                    break;
                }
            }

            dot_pos = -1;
            exp_pos = -1;

            n_pow = 0;
            int exp_sign = 1;

            int digit_count = 0;

            int dummy;
            int dummy_sign;

            int exp_start = -1;;

            // format check
            for (int i = i_start; i < length; i++)
            {
                Char16 c = ptr_source[i];
                //UnityEngine.Debug.Log($"parse format: i={i}, c={c}");
                if (c.IsDigit(out dummy))
                {
                    if (exp_pos == -1)
                    {
                        digit_count++;
                    }
                    // do nothing
                    //UnityEngine.Debug.Log("c is number");
                }
                else
                {
                    if (exp_start > 0 && i >= exp_start)
                    {
                        return(false);                                   // after [e/E] region must be digits.
                    }
                    if (c.IsDot())
                    {
                        if (dot_pos != -1 || dot_pos == i_start + 1)
                        {
                            return(false);
                        }
                        if (exp_pos > 0 && i >= exp_pos)
                        {
                            return(false);
                        }
                        dot_pos = i;
                        //UnityEngine.Debug.Log("c is dot");
                    }
                    else if (c.IsExp())
                    {
                        //UnityEngine.Debug.Log("c is EXP");

                        if (exp_pos != -1)
                        {
                            return(false);                                 // 2nd [e/E] found
                        }
                        if (digit_count == 0 && !zero_head)
                        {
                            return(false);                                 // no digits before [e/E]
                        }
                        if (length - i == 0)
                        {
                            return(false);                                 // no digits after [e/E]
                        }
                        if ((length - i == 1) &&
                            !ptr_source[i + 1].IsDigit(out dummy))
                        {
                            return(false);
                        }

                        exp_pos   = i;
                        exp_start = i + 1;

                        if (ptr_source[i + 1].IsSign(out exp_sign))
                        {
                            if (exp_start + 1 >= length)
                            {
                                return(false);                          // ended by [e/E][+/-]. no pow digits.
                            }
                            exp_start += 1;
                        }
                    }
                    else if (c.IsSign(out dummy_sign))
                    {
                        if (i != exp_pos + 1)
                        {
                            return(false);
                        }
                        exp_start = i + 1;
                    }
                    else
                    {
                        //UnityEngine.Debug.Log("failure to parse");
                        return(false);
                    }
                }
            }

            // decode exp part
            if (exp_start > 0)
            {
                for (int i = 0; i < math.min(length - exp_start, 32); i++) // up to 32 digits for pow
                {
                    n_pow = n_pow * 10 + ptr_source[i + exp_start].ToInt();
                    //UnityEngine.Debug.Log("n_pow(1)=" + n_pow.ToString());
                    if (n_pow > 350)
                    {
                        if (exp_sign < 0)
                        {
                            n_pow = -400;
                            if (dot_pos < 0)
                            {
                                dot_pos = exp_pos;
                            }
                            return(true);  // underflow, round to zero.
                        }
                        else
                        {
                            return(false); // overflow
                        }
                    }
                }
                n_pow *= exp_sign;
            }
            else
            {
                // no [e/E+-[int]] part
                exp_pos = length;
            }

            if (dot_pos < 0)
            {
                dot_pos = exp_pos;
            }

            //UnityEngine.Debug.Log("ParseFloatFormat=" + true.ToString());

            return(true);
        }