Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Native read/write primitive to strings. #97

Closed
JakeWharton opened this issue Dec 24, 2014 · 3 comments
Closed

Native read/write primitive to strings. #97

JakeWharton opened this issue Dec 24, 2014 · 3 comments

Comments

@JakeWharton
Copy link
Member

int and long with radix. Any others? Naming convention?

@swankjesse
Copy link
Member

Existing APIs:

  long readLong();
  long readLongLe();
  String readUtf8(long byteCount);
  Buffer writeLong(long v);
  Buffer writeLongLe(long v);
  Buffer writeUtf8(String string);

We care about a few things:

  • Primitive type. byte, short, int, long. I think we can get away with long only, especially since as I understand it, we'll be using that much memory anyway on 64-bit CPUs.
  • Radix. I think we should do decimal and hexadecimal only. Other radixes aren't common enough to justify being on the fast path, and that way we can put the words decimal and hexadecimal right in the API, instead of using 10 and 16, which read like lengths instead of radixes.
  • Signed vs. Unsigned. I claim that for decimal we start with signed (only), and for hexadecimal we start with unsigned (only). This is particularly important when writing: what should -1L emit? I think for decimal we want "-1" and for hex we want "ffffffffffffffff". Signed hexadecimal is particularly weird and I don't think we need it, at least not in the beginning.

That leaves us with four new methods:

  long readDecimalLong();
  long readHexadecimalUnsignedLong();
  Buffer writeDecimalLong(long value);
  Buffer writeHexadecimalUnsignedLong(long value);

I admit that the method names are wordy! But it gives us the runway should we go mad and later add readDecimalUnsignedLong or readHexadecimalLong.

@JakeWharton
Copy link
Member Author

How does readDecimalLong know when to stop?

@swankjesse
Copy link
Member

We keep reading till we see something not in 0..9, or exhaust the stream.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants