Ejemplo n.º 1
0
 /// <summary>
 /// Expert: allows you to customize the
 /// <see cref="FieldType"/>.
 /// </summary>
 /// <param name="name"> field name </param>
 /// <param name="value"> 32-bit <see cref="int"/> value </param>
 /// <param name="type"> customized field type: must have <see cref="FieldType.NumericType"/>
 ///         of <see cref="NumericType.INT32"/>. </param>
 /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> or <paramref name="type"/> is <c>null</c>. </exception>
 /// <exception cref="ArgumentException">if the field type does not have a
 ///         <see cref="FieldType.NumericType"/> of <see cref="NumericType.INT32"/> </exception>
 public Int32Field(string name, int value, FieldType type)
     : base(name, type)
 {
     if (type.NumericType != Documents.NumericType.INT32)
     {
         throw new ArgumentException("type.NumericType must be NumericType.INT32 but got " + type.NumericType);
     }
     FieldsData = Int32.GetInstance(value);
 }
Ejemplo n.º 2
0
        private string ParseString(string s)
        {
            int readPos  = 0;
            int len      = s.Length;
            int writePos = 0;

            while (readPos < len)
            {
                char c = s[readPos++];
                if (c == '\\')
                {
                    if (readPos >= len)
                    {
                        throw new ArgumentException("Invalid escaped char in [" + s + "]");
                    }
                    c = s[readPos++];
                    switch (c)
                    {
                    case '\\':
                        c = '\\';
                        break;

                    case 'n':
                        c = '\n';
                        break;

                    case 't':
                        c = '\t';
                        break;

                    case 'r':
                        c = '\r';
                        break;

                    case 'b':
                        c = '\b';
                        break;

                    case 'f':
                        c = '\f';
                        break;

                    case 'u':
                        if (readPos + 3 >= len)
                        {
                            throw new ArgumentException("Invalid escaped char in [" + s + "]");
                        }
                        // LUCENENET: Optimized parse so we don't allocate a substring
                        c        = (char)Integer.Parse(s, readPos, 4, radix: 16);
                        readPos += 4;
                        break;
                    }
                }
                @out[writePos++] = c;
            }
            return(new string(@out, 0, writePos));
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Converts a hex string at the specified index and length of <paramref name="hex"/>
        /// into an <see cref="int"/>.
        /// <para/>
        /// NOTE: This was hexToInt() in Lucene
        /// </summary>
        /// <param name="hex">
        /// A string in capital or lower case hex, of no more then 16
        /// characters.
        /// </param>
        /// <param name="startIndex">The index of the first character to begin parsing.</param>
        /// <param name="length">The number of characters to parse.</param>
        /// <exception cref="FormatException">if the string is more than 16 characters long, or if any
        /// character is not in the set [0-9a-fA-f]</exception>
        /// <exception cref="ArgumentOutOfRangeException">
        /// <paramref name="startIndex"/> or <paramref name="length"/> is less than zero.
        /// <para/>
        /// -or-
        /// <para/>
        /// <paramref name="startIndex"/> and <paramref name="length"/> refer to a location outside of <paramref name="hex"/>.
        /// </exception>
        // LUCENENET specific overload
        public static int HexToInt32(string hex, int startIndex, int length)
        {
            if ((length > 16) || !Integer.TryParse(hex, startIndex, length, radix: 16, out int result))
            {
                throw NumberFormatException.Create();
            }

            return(result);
        }
Ejemplo n.º 4
0
        internal static int Get(int i, string s)
        {
            // LUCENENET: Optimized so we don't alocate a substring
            if (!Integer.TryParse(s, i, 1, radix: 10, out int result))
            {
                return(1);
            }

            return(result);
        }
Ejemplo n.º 5
0
        public virtual void TestObjectContains()
        {
            CharArraySet set = new CharArraySet(TEST_VERSION_CURRENT, 10, true);

            J2N.Numerics.Int32 val = J2N.Numerics.Int32.GetInstance(1);
            set.Add(val);
            assertTrue(set.Contains(val));
            assertTrue(set.Contains(J2N.Numerics.Int32.GetInstance(1))); // another integer
            assertTrue(set.Contains("1"));
            assertTrue(set.Contains(new char[] { '1' }));
            // test unmodifiable
            set = CharArraySet.UnmodifiableSet(set);
            assertTrue(set.Contains(val));
            assertTrue(set.Contains(J2N.Numerics.Int32.GetInstance(1))); // another integer
            assertTrue(set.Contains("1"));
            assertTrue(set.Contains(new char[] { '1' }));
        }
Ejemplo n.º 6
0
        internal RegExp ParseRepeatExp()
        {
            RegExp e = ParseComplExp();

            while (Peek("?*+{"))
            {
                if (Match('?'))
                {
                    e = MakeOptional(e);
                }
                else if (Match('*'))
                {
                    e = MakeRepeat(e);
                }
                else if (Match('+'))
                {
                    e = MakeRepeat(e, 1);
                }
                else if (Match('{'))
                {
                    int start = pos;
                    while (Peek("0123456789"))
                    {
                        Next();
                    }
                    if (start == pos)
                    {
                        throw new ArgumentException("integer expected at position " + pos);
                    }
                    // LUCENENET: Optimized so we don't allocate a substring during the parse
                    int n = Integer.Parse(b, start, pos - start, radix: 10);
                    int m = -1;
                    if (Match(','))
                    {
                        start = pos;
                        while (Peek("0123456789"))
                        {
                            Next();
                        }
                        if (start != pos)
                        {
                            // LUCENENET: Optimized so we don't allocate a substring during the parse
                            m = Integer.Parse(b, start, pos - start, radix: 10);
                        }
                    }
                    else
                    {
                        m = n;
                    }
                    if (!Match('}'))
                    {
                        throw new ArgumentException("expected '}' at position " + pos);
                    }
                    if (m == -1)
                    {
                        e = MakeRepeat(e, n);
                    }
                    else
                    {
                        e = MakeRepeat(e, n, m);
                    }
                }
            }
            return(e);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Creates a stored or un-stored <see cref="Int32Field"/> with the provided value
 /// and default <c>precisionStep</c>
 /// <see cref="Util.NumericUtils.PRECISION_STEP_DEFAULT"/> (4).
 /// </summary>
 /// <param name="name"> field name </param>
 /// <param name="value"> 32-bit <see cref="int"/> value </param>
 /// <param name="stored"> <see cref="Field.Store.YES"/> if the content should also be stored </param>
 /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> is <c>null</c>. </exception>
 public Int32Field(string name, int value, Store stored)
     : base(name, stored == Store.YES ? TYPE_STORED : TYPE_NOT_STORED)
 {
     FieldsData = Int32.GetInstance(value);
 }
Ejemplo n.º 8
0
 // TODO: not great but maybe not a big problem?
 /// <summary>
 /// Create a stored-only field with the given <see cref="int"/> value. </summary>
 /// <param name="name"> field name </param>
 /// <param name="value"> <see cref="int"/> value </param>
 /// <exception cref="ArgumentNullException"> if the field <paramref name="name"/> is <c>null</c>. </exception>
 public StoredField(string name, int value)
     : base(name, TYPE)
 {
     FieldsData = Int32.GetInstance(value);
 }
Ejemplo n.º 9
0
        public virtual void TestNumericField()
        {
            using Directory dir = NewDirectory();
            DirectoryReader r = null;

            try
            {
                var numDocs = AtLeast(500);
                var answers = new Number[numDocs];
                using (var w = new RandomIndexWriter(Random, dir))
                {
                    NumericType[] typeAnswers = new NumericType[numDocs];
                    for (int id = 0; id < numDocs; id++)
                    {
                        Document    doc = new Document();
                        Field       nf;
                        Field       sf;
                        Number      answer;
                        NumericType typeAnswer;
                        if (Random.NextBoolean())
                        {
                            // float/double
                            if (Random.NextBoolean())
                            {
                                float f = Random.NextSingle();
                                answer     = Single.GetInstance(f);
                                nf         = new SingleField("nf", f, Field.Store.NO);
                                sf         = new StoredField("nf", f);
                                typeAnswer = NumericType.SINGLE;
                            }
                            else
                            {
                                double d = Random.NextDouble();
                                answer     = Double.GetInstance(d);
                                nf         = new DoubleField("nf", d, Field.Store.NO);
                                sf         = new StoredField("nf", d);
                                typeAnswer = NumericType.DOUBLE;
                            }
                        }
                        else
                        {
                            // int/long
                            if (Random.NextBoolean())
                            {
                                int i = Random.Next();
                                answer     = Int32.GetInstance(i);
                                nf         = new Int32Field("nf", i, Field.Store.NO);
                                sf         = new StoredField("nf", i);
                                typeAnswer = NumericType.INT32;
                            }
                            else
                            {
                                long l = Random.NextInt64();
                                answer     = Int64.GetInstance(l);
                                nf         = new Int64Field("nf", l, Field.Store.NO);
                                sf         = new StoredField("nf", l);
                                typeAnswer = NumericType.INT64;
                            }
                        }
                        doc.Add(nf);
                        doc.Add(sf);
                        answers[id]     = answer;
                        typeAnswers[id] = typeAnswer;
                        FieldType ft = new FieldType(Int32Field.TYPE_STORED);
                        ft.NumericPrecisionStep = int.MaxValue;
                        doc.Add(new Int32Field("id", id, ft));
                        w.AddDocument(doc);
                    }
                    r = w.GetReader();
                } // w.Dispose();

                Assert.AreEqual(numDocs, r.NumDocs);

                foreach (AtomicReaderContext ctx in r.Leaves)
                {
                    AtomicReader      sub = ctx.AtomicReader;
                    FieldCache.Int32s ids = FieldCache.DEFAULT.GetInt32s(sub, "id", false);
                    for (int docID = 0; docID < sub.NumDocs; docID++)
                    {
                        Document doc = sub.Document(docID);
                        Field    f   = doc.GetField <Field>("nf");
                        Assert.IsTrue(f is StoredField, "got f=" + f);
#pragma warning disable 612, 618
                        Assert.AreEqual(answers[ids.Get(docID)], f.GetNumericValue());
#pragma warning restore 612, 618
                    }
                }
            }
            finally
            {
                r?.Dispose();
            }
        }