KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > util > FastParser


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.util;
13
14 /**
15  * Static utility methods for parsing primitives from String's without
16  * creating any objects.
17  */

18 public class FastParser {
19
20     /**
21      * Parse the int at index from value. The int is assumed to run until
22      * the end of the String.
23      */

24     public static int parseInt(String JavaDoc value, int index) {
25         char c = value.charAt(index++);
26         int ans;
27         if (c == '-') ans = - (value.charAt(index++) - '0');
28         else ans = c - '0';
29         int n = value.length();
30         for (; index < n; ) {
31             ans = ans * 10 + (value.charAt(index++) - '0');
32         }
33         return ans;
34     }
35
36     /**
37      * Parse the long at index from value. The long is assumed to run until
38      * the end of the String.
39      */

40     public static long parseLong(String JavaDoc value, int index) {
41         char c = value.charAt(index++);
42         long ans;
43         if (c == '-') ans = - (value.charAt(index++) - '0');
44         else ans = c - '0';
45         int n = value.length();
46         for (; index < n; ) {
47             ans = ans * 10 + (value.charAt(index++) - '0');
48         }
49         return ans;
50     }
51
52 }
53
Popular Tags