Coder Social home page Coder Social logo

unformatter's Introduction

Build Status

unformatter

Java code to parse an input CharSequence, controlled by a format string from a java.util.Formatter.

Similar in concept to a "scanf", but the format String syntax is different. This code is mainly intended to be used with fixed records.

Usage

Say you created a String with the following code

String format = "This is my data: %5d%7x%7o%4s%4.2f values in %%";
String result = String.format(format, 1234, 0x1234, 01234, "1234", 1234.56);

result = "This is my data:  1234   1234   123412341234.56 values in %"

To read back the information:

Unformatter unf = new Unformatter(format);
List<Object> scanned = unf.unformat(result);

scanned will contain the following:

  1. Long 1234
  2. Long 4660 (same as hex 0x1234)
  3. Long 668 (same as octal 01234)
  4. String "1234"
  5. Double 1234.56
  6. CharSequence ""

All parsed values are returned as Longs, Doubles, or Strings as appropriate.

The last element (5 in the above example) represents any remaining data in the input that was not parsed. This is useful if you are reading from a CharSequence containing multiple records.

Supported Format Conversions

Note: Since this if for reading fixed formats, all conversions should include a width!

  • d decimal, returned as a Long
  • f floating point, returned as a Double
  • h hex, returned as a Long
  • o octal, returned as a Long
  • s string, returned as a String
  • % not really a conversion, but "%%" in the format is converted to fixed text "%"
  • n not really a conversion, but "%n" in the format is converted to a fixed text newline

Unsupported Stuff

  • any conversions not noted above, including uppercase
  • argument indexing
  • flags (sorry, no locale-specific grouping for now)

API

For more, consult the code or the zipped javadocs

Unformatter

Unformatter(String format)
  • constructor, taking a format string
List unformat(CharSequence input)
  • parses input,
  • returns a List contain Longs, Doubles, or Strings for the data.
  • the last element in the List is a CharSequence of any reminaing input.

unformatter's People

Contributors

morganconrad avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.