diff options
author | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-15 18:54:04 +0000 |
---|---|---|
committer | qsr@chromium.org <qsr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2014-05-15 18:54:04 +0000 |
commit | 230db69d88522c52638fe436b35f1314b0b83fe4 (patch) | |
tree | 94925144f1f975cf3bcd5cd381d292590c6d6d18 /mojo/bindings | |
parent | 8bb5a3434d475e98a1bab6d6183096360d3eb67c (diff) | |
download | chromium_src-230db69d88522c52638fe436b35f1314b0b83fe4.zip chromium_src-230db69d88522c52638fe436b35f1314b0b83fe4.tar.gz chromium_src-230db69d88522c52638fe436b35f1314b0b83fe4.tar.bz2 |
Helper classes for java bindings.
Review URL: https://codereview.chromium.org/288133005
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@270757 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'mojo/bindings')
-rw-r--r-- | mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java b/mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java new file mode 100644 index 0000000..f2aa046 --- /dev/null +++ b/mojo/bindings/java/src/org/chromium/mojo/bindings/BindingsHelper.java @@ -0,0 +1,64 @@ +// Copyright 2014 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +package org.chromium.mojo.bindings; + +/** + * Helper functions. + */ +public class BindingsHelper { + /** + * Alignment in byte for mojo serialization. + */ + public static final int ALIGNMENT = 8; + + /** + * Align |size| on {@link BindingsHelper#ALIGNMENT}. + */ + public static int align(int size) { + return (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1); + } + + /** + * Compute the size in bytes of the given string encoded as utf8. + */ + public static int utf8StringSizeInBytes(String s) { + int res = 0; + for (int i = 0; i < s.length(); ++i) { + char c = s.charAt(i); + int codepoint = c; + if (isSurrogate(c)) { + i++; + char c2 = s.charAt(i); + codepoint = Character.toCodePoint(c, c2); + } + res += 1; + if (codepoint > 0x7f) { + res += 1; + if (codepoint > 0x7ff) { + res += 1; + if (codepoint > 0xffff) { + res += 1; + if (codepoint > 0x1fffff) { + res += 1; + if (codepoint > 0x3ffffff) { + res += 1; + } + } + } + } + } + } + return res; + } + + /** + * Determines if the given {@code char} value is a Unicode <i>surrogate code unit</i>. See + * {@link Character#isSurrogate}. Extracting here because the method only exists at API level + * 19. + */ + private static boolean isSurrogate(char c) { + return c >= Character.MIN_SURROGATE && c < (Character.MAX_SURROGATE + 1); + } +} |