While fixing some old code I came up with a class that parses floats from a string.

This is used for parsing svg files in xml format. Floats can be separated by comma, space or anything at all in case the next number starts with a sign. They can also use exponential notation.


<path d="

This is the class:

private class ParserData
    String s;
    int cur = 0;
    int len = 0;

    float prev_x = .0f;
    float prev_y = .0f;

    float prev_cx = .0f;
    float prev_cy = .0f;

    float start_x = .0f;
    float start_y = .0f;

    char prev_curve;
    char next_curve = 0;

    public void setData(String data)
        s = data;
        len = s.length();

        cur = 0;
        prev_x = 0;
        prev_y = 0;

        prev_cx = .0f;
        prev_cy = .0f;

        start_x = .0f;
        start_y = .0f;

        prev_curve = 0;
        next_curve = 0;         

    public boolean hasNext()
        return cur < len;

    public char getNext()
        return s.charAt(cur++);

    public char peekNext()
        return s.charAt(cur);

    public void skipSpaces()
            char c = s.charAt(cur);

            case ' ':
            case ',':
            case '\n':
            case '\t':
            case '\r':



    private float parseFloat()
        boolean exp = false;
        boolean last = false;


        int j = cur;

        if(s.charAt(j) == '-')

        while(hasNext() && !last)
            char c = getNext();

                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                case '+':
                case '.':
                    exp = false;

                case '-':
                        exp = false;
                        return Uti.safeParseFloat(s.substring(j, cur));

                case 'e':
                case 'E':
                    exp = true;

                    return Uti.safeParseFloat(s.substring(j, cur));

        return Uti.safeParseFloat(s.substring(j, cur));

While hasNext is true getNext is used to read the type of element, then parseFloat is called to get all the points and so on. For example, "c" is a cubic bezier curve and after it there are 3 control points (3 comma separated pairs, or 6 floats) to read.

I'm looking for some ways to speed up the process.

Here is the full code of the content handler.

  • \$\begingroup\$ Can you add a code example for how you use this class? \$\endgroup\$ – tim Oct 6 '14 at 19:03
  • \$\begingroup\$ It is used in the handler of a sax parser, here is the code: pastebin.com/r7VM7NSp \$\endgroup\$ – キキジキ Oct 6 '14 at 19:17

At least for now, I did not look at improving performance or at the general approach, but at some more basic things I think would improve the readability of the code.

setData vs Constructor

It seems to me that setData takes the role of a constructor here. In that case, I would just make it a constructor instead of making it a method. Sure, this would result in the creation of a new object, but it would result in cleaner code (the ParserData would always be a valid object).


First of all, in Java CamelCase is used instead of underscores. It's always good to follow the conventions of a language, so prev_curve for example should be prevCurve.

Variables should always have expressive names, but this is especially important at class level. Your names wouldn't tell me the difference between prev_x and prev_cx for example. s is also not a very expressive name (I cannot think of a name that's a lot better, but even string or inputString would be better. For consistency, data should then be changed to the same name).

len could be length, cur currentPosition, and c could be currentChar.

exp should probably be exponent and j also should get a more expressive name (something like startFloatString).

access modifiers

Your fields should be private.

Your skipSpaces should probably be private as well.

The parseFloat method is private, and not called from within the class. But it is the main parse method. This seems wrong, I would make it public.

Unused Fields

Defining fields and then not using them is not good practice. You should remove next_curve, prev_curve, start_y, start_x, prev_cy, prev_cx, prev_y and prev_x. I see that you do use these fields in a different context, but not in your parsing code. So I would create a new class for them instead of tagging them onto this one.

You should also remove last and change

while(hasNext() && !last)



Use methods that you have defined

You have getNext, so why not use it in skipSpaces instead of getting the character yourself and then increasing cur.

Same with peekNext in parseFloat.


In Java, it is customary to put the { on the same line, not the next.

  • \$\begingroup\$ Thank you for the feedback, I updated my question with the updated code. \$\endgroup\$ – キキジキ Oct 6 '14 at 20:24
  • 1
    \$\begingroup\$ @キキジキ sure, glad to help :) But I have to role back your edit (please see What you can and cannot do after receiving answers). You could wait for some more answers, and then post a follow-up question. \$\endgroup\$ – tim Oct 6 '14 at 20:25
  • \$\begingroup\$ Oh sorry, got it. \$\endgroup\$ – キキジキ Oct 6 '14 at 20:30

Parsing character by character is going to be slow. Perhaps you could consider alternative implementations:

  • Split the string with a regular expressions
  • Use a regular expression to match the numbers and iterate over the matches

Here's an example:

public static List<Float> parse(String data) {
    String[] parts = data.replaceAll("[^\\d.-]|(?=-)", " ").trim().split(" +");
    List<Float> numbers = new ArrayList<>(parts.length);
    for (String s : parts) {
    return numbers;

For the sample you gave, this method will return a list with numbers:

 245.792, 247.275, -0.828, 1.036, 0.794, 2.333, 0.924, 3.53, 0.161, 1.442, ...

For other approaches using regular expressions, see this related question on stackoverflow (I asked just for this question ;-)

  • \$\begingroup\$ I also need to get the letters that define curves inside the path, like "c" "C" "s" "S" ... For example, when a "c" comes I read the next 6 floats and generate a cubic spline, "v" I read one value and add a vertical line and so on. \$\endgroup\$ – キキジキ Oct 8 '14 at 22:40
  • \$\begingroup\$ It's probably not a good test but, I tried ignoring the problem above and feeding a file with only numbers to both versions. With regexp I get 2.5x worse performance. Source and data file -> pastebin.com/nV9eaaFW pastebin.com/ZUtgdvEY \$\endgroup\$ – キキジキ Oct 8 '14 at 23:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.