Friday, March 15, 2013

Displaying ip addresses in postgres

If you have ipv4 addresses stored as integers in a postgres database, you can display the address using the trick '0.0.0.0'::inet + ip. For example:


     select distinct ('0.0.0.0'::inet + devaddr) as addr from interface
        order by addr;



Friday, December 22, 2006

Some features of Ant 1.7.0

Ant 1.7.0 has just been released. It includes a long
list of fixes and it includes a new resource framework.

Some of the new features that have not got highlighed
are:
  1. Support for junit.jar in the <junit> classpath element, this means that there is now no need to place the junit.jar in $ANT_HOME/lib.
  2. Support for junit4.
  3. Support for javax scripting. This is done by using a 'manager="javax"' attribute in the <script> task.
  4. Scripting language jars can now be placed in the <script> classpath element. This means that these jars do not need to be placed in $ANT_HOME/lib. (This does not work for javascript and jython jars - will be fixed in Ant 1.7.1)
  5. Script support for conditions, filters, mappers, selectors added.
  6. Antversion condition added - scripts can how make sure that they are run by a supported version of ant.
  7. Memory leakages plugged for <*ant*>

Tuesday, November 28, 2006

Javax scripting for ant 1.7.0

It looks like I am be able to persuade my ant
collegues to have javax.scripting in ant 1.7.0.

There are two options to specify javax.scripting
rather than bsf, have an extra attribute on the script
task:
<script language="javascript" engine="jsr">
print("hello world\n");
</script>
or use a prefix in the language tag:
<script language="jsr:javascript">
print("hello world\n");
</script>

The nice thing is that with this, one can use
javascript scripting in ant with java6 *without* any configuration.

Monday, November 27, 2006

Multiline strings in java - more python like

I have modified the python string patch so that the patch
behaves more like python.
r" is now a raw string
ur" is now a string that denotes a raw string with unicode
escape handing.

On a side note, I see that F3 has a JSR222 implemenation,
http://blogs.sun.com/chrisoliver/entry/f3_and_jsr-223
the example given is:
String script =
"import java.util.Date;" +
"import java.lang.System;" +
" " +
"System.out.println(now:Date);";
engine.eval(script);


This could be written a little nicer like (this adds in new lines):
String script = """
import java.util.Date;
import java.lang.System;

System.out.println(now:Date);
""";
engine.eval(script);

Sunday, November 26, 2006

Multiline strings in java - Part 2

After a little more messing around with Scanner.java in the
compiler source code, I now have the string handling similar to
pythons. """ denotes a multiline string, the code normalizes
end of line characters (CRNL -> NL) (CR -> NL) (a bit like XML
handling), and raw strings are added. r" or r""" denotes a raw string
with unicode escape handling (equilvant to pythons ur", ur""")
and R" or R""" denotes a raw string without unicode handling.

Raw strings are usefull for regular expressions and
for evalulating script strings.

The grammar is something like: (copied in part from python grammar)

stringliteral ::= [stringprefix](shortstring | longstring)
stringprefix ::= "r" | "R"
shortstring ::= '"' shortstringitem* '"'
longstring ::= '"""' longstringitem* '"""'
shortstringitem ::= shortstringchar | escapeseq
longstringitem ::= longstringchar | escapeseq
shortstringchar ::=
longstringchar ::=
escapeseq ::= "\"

So the following now compiles and runs:

import javax.script.*;

public class ScriptExample {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine engine = m.getEngineByName("javascript");
engine.eval(r"""
print("This is the world ");
print("calling\n");
""");
String x = R"\u";
System.out.println("No escape " + x);
}
}

/f/src/compiler > java -jar dist/lib/javac.jar ScriptExample.java
/f/src/compiler > java -cp . ScriptExample
This is the world calling
No escape \u


The code is *NOT* fully tested .... but
here is the diff file (-Nua) for Scanner.java to Scanner.java.orig

--- Scanner.java.orig Sun Nov 26 12:57:25 2006
+++ Scanner.java Sun Nov 26 21:49:59 2006
@@ -52,6 +52,13 @@
@Version("@(#)Scanner.java 1.73 06/11/11")
public class Scanner implements Lexer {

+ /** Enumerated type on how to handle decoding \ in string */
+ private enum EscapeHandling {
+ FULL, // decode all \
+ UNICODE, // only decode unicode escape
+ RAW, // completely raw
+ }
+
private static boolean scannerDebug = false;

/** A factory for creating scanners. */
@@ -291,6 +298,101 @@
convertUnicode();
}
}
+ /** Read next character.
+ * @param escapeHandling how to handle \ in strings
+ */
+ private void scanChar(EscapeHandling escapeHandling) {
+ ch = buf[++bp];
+ if (escapeHandling != EscapeHandling.RAW && ch == '\\') {
+ convertUnicode();
+ }
+ }
+
+ /**
+ * Look ahead for a character.
+ * @param count the number to look ahead by.
+ * @return the character of the future... or (char) 0 for
+ * characters beyond the buffer.
+ */
+ private char peekAhead(int count) {
+ if (count + bp >= buflen) {
+ return (char) 0;
+ }
+ return buf[count + bp];
+ }
+
+ /** Check if we are at a triple double quote.
+ * @return true if the next two characters are quotes.
+ */
+ private boolean atTripleQuote() {
+ return peekAhead(1) == '"' && peekAhead(2) == '"';
+ }
+
+ /** Scan a triple quoted string.
+ * @param escapeHandling how to handle \ in strings
+ */
+ private void scanTripleQuotedString(EscapeHandling escapeHandling) {
+ // read in the """ characters
+ scanChar();
+ scanChar();
+ scanChar(escapeHandling);
+ while (true) {
+ while (ch != '"' && ch != CR && bp < buflen) {
+ scanLitChar(escapeHandling);
+ }
+ if (bp >= buflen) {
+ lexError(pos, "unclosed.str.lit");
+ return;
+ }
+ if (ch == CR) {
+ if (peekAhead(1) == LF) {
+ scanChar();
+ continue; // Normalize CRLF to LF
+ }
+ ch = LF; // Normalize CR to LF
+ continue;
+ }
+ // Character is a ", is the the triple?
+ if (atTripleQuote()) {
+ // yes - end of the string
+ scanChar();
+ scanChar();
+ scanChar();
+ token = STRINGLITERAL;
+ return;
+ } else {
+ // no - add it to the string.
+ scanLitChar(escapeHandling);
+ }
+ }
+ }
+
+ /** Scan a quoted string.
+ * @param escapeHandling how to handle \ in strings
+ */
+ private void scanQuotedString(EscapeHandling escapeHandling) {
+ scanChar(escapeHandling);
+ while (ch != '"' && ch != CR && ch != LF && bp < buflen) {
+ scanLitChar(escapeHandling);
+ }
+ if (ch == '"') {
+ token = STRINGLITERAL;
+ scanChar();
+ } else {
+ lexError(pos, "unclosed.str.lit");
+ }
+ }
+
+ /** Scan a string.
+ * @param escapeHandling how to handle \ in strings
+ */
+ private void scanString(EscapeHandling escapeHandling) {
+ if (atTripleQuote()) {
+ scanTripleQuotedString(escapeHandling);
+ } else {
+ scanQuotedString(escapeHandling);
+ }
+ }

/** Read next character in comment, skipping over double '\' characters.
*/
@@ -323,9 +425,10 @@
}

/** Read next character in character or string literal and copy into sbuf.
+ * @param escapeHandling how to handle \ in strings
*/
- private void scanLitChar() {
- if (ch == '\\') {
+ private void scanLitChar(EscapeHandling escapeHandling) {
+ if (escapeHandling == EscapeHandling.FULL && ch == '\\') {
if (buf[bp+1] == '\\' && unicodeConversionBp != bp) {
bp++;
putChar('\\');
@@ -369,7 +472,8 @@
}
}
} else if (bp != buflen) {
- putChar(ch); scanChar();
+ putChar(ch);
+ scanChar(escapeHandling);
}
}

@@ -779,16 +883,30 @@
endPos = bp;
processLineTerminator();
break;
+ case 'r':
+ if ('"' == peekAhead(1)) {
+ scanChar();
+ scanString(EscapeHandling.UNICODE);
+ return;
+ }
+ // Fall Tru
+ case 'R':
+ if ('"' == peekAhead(1)) {
+ scanChar();
+ scanString(EscapeHandling.RAW);
+ return;
+ }
+ // FALL TRU
case 'A': case 'B': case 'C': case 'D': case 'E':
case 'F': case 'G': case 'H': case 'I': case 'J':
case 'K': case 'L': case 'M': case 'N': case 'O':
- case 'P': case 'Q': case 'R': case 'S': case 'T':
+ case 'P': case 'Q': case 'S': case 'T':
case 'U': case 'V': case 'W': case 'X': case 'Y':
case 'Z':
case 'a': case 'b': case 'c': case 'd': case 'e':
case 'f': case 'g': case 'h': case 'i': case 'j':
case 'k': case 'l': case 'm': case 'n': case 'o':
- case 'p': case 'q': case 'r': case 's': case 't':
+ case 'p': case 'q': case 's': case 't':
case 'u': case 'v': case 'w': case 'x': case 'y':
case 'z':
case '$': case '_':
@@ -902,7 +1020,7 @@
} else {
if (ch == CR || ch == LF)
lexError(pos, "illegal.line.end.in.char.lit");
- scanLitChar();
+ scanLitChar(EscapeHandling.FULL);
if (ch == '\'') {
scanChar();
token = CHARLITERAL;
@@ -911,17 +1029,9 @@
}
}
return;
- case '\"':
- scanChar();
- while (ch != '\"' && ch != CR && ch != LF && bp < buflen)
- scanLitChar();
- if (ch == '\"') {
- token = STRINGLITERAL;
- scanChar();
- } else {
- lexError(pos, "unclosed.str.lit");
- }
- return;
+ case '"':
+ scanString(EscapeHandling.FULL);
+ return;
default:
if (isSpecial(ch)) {
scanOperator();

Saturday, November 25, 2006

Multiline strings in java - solved (sort of)

There has been an long outstanding request for
multiline stings in java. See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4472509
and http://www.mrchucho.net/index.php/archives/2005/06/01/multi-line-strings-in-java/

With all the new 1.5 stuff being added - annotations, printf, varargs
it is quite annoying that java still has to do
"aaa\n"
+ "bbb\n"
...
for multiline strings.
There is especially annoying when - writing unit tests (xml strings for example)
and for doc and SQL annotations.
With JSR Scripting being added in jdk6, it has become completely stupid,
It would be nice to something like the pythonic -

engine = manager.getEngineByName("beanshell");
engine.eval("""
map = new HashMap();
map.put("foo", "bar");

importObject( map );

print( get("foo") ); // bar
""");

With the GPL release of javac, one can have a look at the
javac code - and see what needs to be done to add this much
needed extension.

A quick look at the code shows that it should be relatively easy
to add this - but I do not have the time at the moment
to add """.

However, one can add multiline strings easily
by relaxing the check for NL and CR.

$COMPILER_HOME/src/share/classes/com/sun/tools/javac/parser/Scanner.java
Line: 916 Change to
while (ch != '\"' /*&& ch != CR && ch != LF*/ && bp < buflen)

and presto the following compiles:

public class HelloWorld {
public static void main(String[] args) {
System.out.println("
Hello world
");
}
}

/f/src/compiler > java -jar dist/lib/javac.jar HelloWorld.java
/f/src/compiler > java -cp . HelloWorld

Hello world

/f/src/compiler >

The script example is a little ropy as one needs to escape
the " and \.

import javax.script.*;

public class ScriptExample {
public static void main(String[] args) throws Exception {
ScriptEngineManager m = new ScriptEngineManager();
ScriptEngine engine = m.getEngineByName("javascript");
engine.eval("
print(\"This is the world \");
print(\"calling\\n\");
");
}
}

/f/src/compiler > java -jar dist/lib/javac.jar ScriptExample.java
/f/src/compiler > java -cp . ScriptExample
This is the world calling
/f/src/compiler >

Tuesday, August 15, 2006

Some xml:

<abc>
</abc>