git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[EGIT PATCH 03/26] Add Constants.encode as a utility for quick encoding in UTF-8

From
Shawn O. Pearce <spearce@spearce.org>
Date
Aug 12, 2008, 01:07 UTC
Message-ID
<1218503293-14057-4-git-send-email-spearce@spearce.org>
In-Reply-To
<1218503293-14057-3-git-send-email-spearce@spearce.org>

We often need to convert a string into a UTF-8 encoding, so that we can use this string as a path filter in a TreeWalk or in some other suitable place where we assume a standard UTF-8 encoding is being used. As we have already done the lookup for the CHARSET we can reuse that same CHARSET reference during future encoding calls, while allowing the CharSet implementation to cache and reuse the actual encoder instance.

Whenever possible we try to avoid copying the result as most of the time the returned ByteBuffer's internal array matches the result array we need to return to our caller.

Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
---
 .../spearce/jgit/lib/ConstantsEncodingTest.java    |   89 ++++++++++++++++++++
 .../src/org/spearce/jgit/lib/Constants.java        |   25 ++++++
 2 files changed, 114 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConstantsEncodingTest.java
diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConstantsEncodingTest.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConstantsEncodingTest.java
new file mode 100644
index 0000000..7b3e5a0
--- /dev/null
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/ConstantsEncodingTest.java
@@ -0,0 +1,89 @@
+/*
+ * Copyright (C) 2008, Google Inc.
+ *
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or
+ * without modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * - Redistributions of source code must retain the above copyright
+ *   notice, this list of conditions and the following disclaimer.
+ *
+ * - Redistributions in binary form must reproduce the above
+ *   copyright notice, this list of conditions and the following
+ *   disclaimer in the documentation and/or other materials provided
+ *   with the distribution.
+ *
+ * - Neither the name of the Git Development Community nor the
+ *   names of its contributors may be used to endorse or promote
+ *   products derived from this software without specific prior
+ *   written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
+ * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
+ * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.spearce.jgit.lib;
+
+import java.io.UnsupportedEncodingException;
+import java.util.Arrays;
+
+import junit.framework.TestCase;
+
+public class ConstantsEncodingTest extends TestCase {
+	public void testEncodeASCII_SimpleASCII()
+			throws UnsupportedEncodingException {
+		final String src = "abc";
+		final byte[] exp = { 'a', 'b', 'c' };
+		final byte[] res = Constants.encodeASCII(src);
+		assertTrue(Arrays.equals(exp, res));
+		assertEquals(src, new String(res, 0, res.length, "UTF-8"));
+	}
+
+	public void testEncodeASCII_FailOnNonASCII() {
+		final String src = "Ūnĭcōde̽";
+		try {
+			Constants.encodeASCII(src);
+			fail("Incorrectly accepted a Unicode character");
+		} catch (IllegalArgumentException err) {
+			assertEquals("Not ASCII string: " + src, err.getMessage());
+		}
+	}
+
+	public void testEncodeASCII_Number13() {
+		final long src = 13;
+		final byte[] exp = { '1', '3' };
+		final byte[] res = Constants.encodeASCII(src);
+		assertTrue(Arrays.equals(exp, res));
+	}
+
+	public void testEncode_SimpleASCII() throws UnsupportedEncodingException {
+		final String src = "abc";
+		final byte[] exp = { 'a', 'b', 'c' };
+		final byte[] res = Constants.encode(src);
+		assertTrue(Arrays.equals(exp, res));
+		assertEquals(src, new String(res, 0, res.length, "UTF-8"));
+	}
+
+	public void testEncode_Unicode() throws UnsupportedEncodingException {
+		final String src = "Ūnĭcōde̽";
+		final byte[] exp = { (byte) 0xC5, (byte) 0xAA, 0x6E, (byte) 0xC4,
+				(byte) 0xAD, 0x63, (byte) 0xC5, (byte) 0x8D, 0x64, 0x65,
+				(byte) 0xCC, (byte) 0xBD };
+		final byte[] res = Constants.encode(src);
+		assertTrue(Arrays.equals(exp, res));
+		assertEquals(src, new String(res, 0, res.length, "UTF-8"));
+	}
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
index 7c2cef9..23ac3ac 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Constants.java
@@ -1,6 +1,7 @@
 /*
  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
+ * Copyright (C) 2008, Google Inc.
  *
  * All rights reserved.
  *
@@ -38,6 +39,7 @@
 
 package org.spearce.jgit.lib;
 
+import java.nio.ByteBuffer;
 import java.nio.charset.Charset;
 import java.security.MessageDigest;
 import java.security.NoSuchAlgorithmException;
@@ -387,6 +389,29 @@ public final class Constants {
 		return r;
 	}
 
+	/**
+	 * Convert a string to a byte array in the standard character encoding.
+	 * 
+	 * @param str
+	 *            the string to convert. May contain any Unicode characters.
+	 * @return a byte array representing the requested string, encoded using the
+	 *         default character encoding (UTF-8).
+	 * @see #CHARACTER_ENCODING
+	 */
+	public static byte[] encode(final String str) {
+		final ByteBuffer bb = Constants.CHARSET.encode(str);
+		final int len = bb.limit();
+		if (bb.hasArray() && bb.arrayOffset() == 0) {
+			final byte[] arr = bb.array();
+			if (arr.length == len)
+				return arr;
+		}
+
+		final byte[] arr = new byte[len];
+		bb.get(arr);
+		return arr;
+	}
+
 	static {
 		if (OBJECT_ID_LENGTH != newMessageDigest().getDigestLength())
 			throw new LinkageError("Incorrect OBJECT_ID_LENGTH.");
-- 
1.6.0.rc2.22.g71b99
Previous: Shawn O. PearceNext: Shawn O. Pearce
Message 4 of 26 in “New DirCache API”
  1. 00/26 New DirCache APIShawn O. Pearce, Aug 12, 2008
  2. 01/26 Force all source code to UTF-8 encoding by defaultShawn O. Pearce, Aug 12, 2008
  3. 02/26 Protect WorkingTreeIterator's name encoding from weird ByteBuffersShawn O. Pearce, Aug 12, 2008
  4. 03/26 Add Constants.encode as a utility for quick encoding in UTF-8Shawn O. Pearce, Aug 12, 2008
  5. 04/26 Rely upon Constants.CHARSET over Constants.CHARACTER_ENCODINGShawn O. Pearce, Aug 12, 2008
  6. 05/26 Allow AbstractTreeIterators to find out about StopWalkExceptionsShawn O. Pearce, Aug 12, 2008
  7. 06/26 Implement a new .git/index (aka dircache) read interfaceShawn O. Pearce, Aug 12, 2008
  8. 07/26 Export the new DirCache API to Eclipse plugins using jgitShawn O. Pearce, Aug 12, 2008
  9. 08/26 Support locking (and unlocking) a .git/index through DirCacheShawn O. Pearce, Aug 12, 2008
  10. 09/26 Support writing a .git/index through DirCacheShawn O. Pearce, Aug 12, 2008
  11. 10/26 Support the 'TREE' extension in .git/index through DirCacheShawn O. Pearce, Aug 12, 2008
  12. 11/26 Support using a DirCache within a TreeWalkShawn O. Pearce, Aug 12, 2008
  13. 12/26 Support recreating a .git/index through DirCacheShawn O. Pearce, Aug 12, 2008
  14. 13/26 Support iterating and building a DirCache at the same timeShawn O. Pearce, Aug 12, 2008
  15. 14/26 Support creating a new DirCacheEntry for an arbitrary pathShawn O. Pearce, Aug 12, 2008
  16. 15/26 Support a simplified model of editing index entriesShawn O. Pearce, Aug 12, 2008
  17. 16/26 Support recursively getting all entries under a subtree pathShawn O. Pearce, Aug 12, 2008
  18. 17/26 Support copying meta fields from one DirCacheEntry to anotherShawn O. Pearce, Aug 12, 2008
  19. 18/26 Add JUnit tests for new DirCache APIShawn O. Pearce, Aug 12, 2008
  20. 20/26 Allow the new DirCacheIterator in command line argumentsShawn O. Pearce, Aug 12, 2008
  21. 21/26 Add debugging commands to interact with the new DirCache codeShawn O. Pearce, Aug 12, 2008
  22. 22/26 Add a basic command line implementation of rmShawn O. Pearce, Aug 12, 2008
  23. 23/26 Rewrite GitMoveDeleteHook to use DirCacheBuilderShawn O. Pearce, Aug 12, 2008
  24. 24/26 Teach GitMoveDeleteHook how to move a folder recursivelyShawn O. Pearce, Aug 12, 2008
  25. 25/26 Rewrite UntrackOperation to use DirCacheBuilderShawn O. Pearce, Aug 12, 2008
  26. 26/26 Rewrite AssumeUnchangedOperation to use DirCacheShawn O. Pearce, Aug 12, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.