KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > derby > SearchUtil


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.derby;
21
22 /**
23  *
24  * @author pj97932
25  */

26 public class SearchUtil {
27
28     static final int FOUND = -1;
29
30     /** Returns the number of characters from the searchedFor string that are at the end of the buffer,
31      * or FOUND if found the whole string
32      **/

33     static int checkForString(String JavaDoc searchedFor, int searchStart, char[] buf, int bufLen) {
34         if (searchedFor.length() == 0)
35             throw new IllegalArgumentException JavaDoc();
36         if (searchStart > 0) {
37             // already have a substring
38
int res = checkPosition(searchedFor, searchStart, buf, bufLen, 0);
39             if (res == FOUND)
40                 return FOUND;
41         }
42         for (int i = 0; i < bufLen; i++) {
43             if (buf[i] == searchedFor.charAt(searchStart)) {
44                 // found the first character
45
int res = checkPosition(searchedFor, 0, buf, bufLen, i);
46                 if (res != 0)
47                     return res;
48             }
49         }
50         return 0;
51     }
52
53     /** Checks whether the buffer contains a portion of searchedFor from searchStart, starting from buffer
54      * position bufFrom.
55      * Returns the number of characters from the searchedFor string that are at the end of the buffer,
56      * or FOUND if found the whole string
57      */

58     static int checkPosition(String JavaDoc searchedFor, int searchStart, char[] buf, int bufLen, int bufFrom) {
59         String JavaDoc realSearch = searchedFor.substring(searchStart);
60         for (int i = 0; i < realSearch.length(); i++) {
61             int bufPos = bufFrom + i;
62             if (bufPos >= bufLen) {
63                 return searchStart + i;
64             }
65             if (buf[bufPos] != realSearch.charAt(i))
66                 return 0;
67         }
68         return FOUND;
69     }
70         
71     
72 }
73
Popular Tags