Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
StringHelper.cs
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Util
21 {
22 
23 
24  /// <summary> Methods for manipulating strings.</summary>
25  public abstract class StringHelper
26  {
27  /// <summary> Expert:
28  /// The StringInterner implementation used by Lucene.
29  /// This shouldn't be changed to an incompatible implementation after other Lucene APIs have been used.
30  /// </summary>
31  public static StringInterner interner = new SimpleStringInterner(1024, 8);
32 
33  /// <summary>Return the same string object for all equal strings </summary>
34  public static System.String Intern(System.String s)
35  {
36  return interner.Intern(s);
37  }
38 
39  /// <summary> Compares two byte[] arrays, element by element, and returns the
40  /// number of elements common to both arrays.
41  ///
42  /// </summary>
43  /// <param name="bytes1">The first byte[] to compare
44  /// </param>
45  /// <param name="len1"></param>
46  /// <param name="bytes2">The second byte[] to compare
47  /// </param>
48  /// <param name="len2"></param>
49  /// <returns> The number of common elements.
50  /// </returns>
51  public static int BytesDifference(byte[] bytes1, int len1, byte[] bytes2, int len2)
52  {
53  int len = len1 < len2?len1:len2;
54  for (int i = 0; i < len; i++)
55  if (bytes1[i] != bytes2[i])
56  return i;
57  return len;
58  }
59 
60  /// <summary> Compares two strings, character by character, and returns the
61  /// first position where the two strings differ from one another.
62  ///
63  /// </summary>
64  /// <param name="s1">The first string to compare
65  /// </param>
66  /// <param name="s2">The second string to compare
67  /// </param>
68  /// <returns> The first position where the two strings differ.
69  /// </returns>
70  public static int StringDifference(System.String s1, System.String s2)
71  {
72  int len1 = s1.Length;
73  int len2 = s2.Length;
74  int len = len1 < len2?len1:len2;
75  for (int i = 0; i < len; i++)
76  {
77  if (s1[i] != s2[i])
78  {
79  return i;
80  }
81  }
82  return len;
83  }
84 
85  private StringHelper()
86  {
87  }
88  }
89 }