X-Git-Url: https://oss.titaniummirror.com/gitweb?a=blobdiff_plain;f=support%2Fsdk%2Fjava%2Fnet%2Ftinyos%2Fpacket%2FParseArgs.java;fp=support%2Fsdk%2Fjava%2Fnet%2Ftinyos%2Fpacket%2FParseArgs.java;h=5e9ed3ae15c221fcc0614e6e3a5ae96d54ea66bc;hb=1ba974b83d19fc41bf80acd52726f36f7f1df297;hp=0000000000000000000000000000000000000000;hpb=4db69a460ad5d18d33cbf5c3ef74ad584d9e2886;p=tinyos-2.x.git diff --git a/support/sdk/java/net/tinyos/packet/ParseArgs.java b/support/sdk/java/net/tinyos/packet/ParseArgs.java new file mode 100644 index 00000000..5e9ed3ae --- /dev/null +++ b/support/sdk/java/net/tinyos/packet/ParseArgs.java @@ -0,0 +1,41 @@ + /** + * Parse a string into tokens based on a sequence of delimiters + * Given delimiters (single characters) d1, d2, ..., dn, this + * class recognises strings of the form s0[d1s1][d2s2]...[dnsn], + * where s does not contain character di + * This is unambiguous if all di are distinct. If not, strings + * are attributed to the earliest possible si (so if the delimiters + * are : and :, and the input string is foo:bar, then s0 is foo, + * s1 is bar and s2 is null + */ +package net.tinyos.packet; + +class ParseArgs { + String tokens[]; + int tokenIndex; + + ParseArgs(String s, String delimiterSequence) { + int count = delimiterSequence.length(); + tokens = new String[count + 1]; + tokenIndex = 0; + + // Fill in the tokens + int i = 0, lastMatch = 0; + while (i < count) { + int pos = s.indexOf(delimiterSequence.charAt(i++)); + + if (pos >= 0) { + // When we finally find a delimiter, we know where + // the last token ended + tokens[lastMatch] = s.substring(0, pos); + lastMatch = i; + s = s.substring(pos + 1); + } + } + tokens[lastMatch] = s; + } + + String next() { + return tokens[tokenIndex++]; + } +}