File: StringLengthFunction.java

package info (click to toggle)
libxt-java 0.19991105-5
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,908 kB
  • ctags: 2,762
  • sloc: java: 12,823; makefile: 52; xml: 46
file content (28 lines) | stat: -rw-r--r-- 719 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package com.jclark.xsl.expr;

import com.jclark.xsl.om.*;

class StringLengthFunction extends FunctionOpt1 {
  ConvertibleExpr makeCallExpr(ConvertibleExpr e) {
    final StringExpr se = e.makeStringExpr();
    return new ConvertibleNumberExpr() {
      public double eval(Node node, ExprContext context) throws XSLException {
	return stringLength(se.eval(node, context));
      }
    };
  }

  private final static boolean isLowSurrogate(char c) {
    return (c & 0xFC00) == 0xD800;
  }

  private final static int stringLength(String s) {
    int n = s.length();
    int len = n;
    for (int i = 0; i < n; i++) {
      if (isLowSurrogate(s.charAt(i)))
	--len;
    }
    return len;
  }
}