static char[] bufOf(String s) { try { return Unsafe.bufOf(s); } catch (Exception e) { return s.toCharArray(); } }
static char[] bufOf(String s) { try { return Unsafe.bufOf(s); } catch (Exception e) { return s.toCharArray(); } }
public RequestHandler findStaticGetHandler(String url) { Iterator<String> path = Path.tokenizer(Unsafe.bufOf(url)); Node node = root(H.Method.GET); while (null != node && path.hasNext()) { String nodeName = path.next(); node = node.staticChildren.get(nodeName); if (null == node || node.terminateRouteSearch()) { break; } } return null == node ? null : node.handler; }
public String with(String replacement) { if (null != pattern) { return pattern.matcher(text).replaceAll(replacement); } else { if (text.length() < keyword.length()) { return text; } int firstId = this.text.indexOf(this.keyword); if (firstId < 0) { return this.text; } char[] text = Unsafe.bufOf(this.text); char[] target = this.keyword.toCharArray(); char[] replace = replacement.toCharArray(); char[] result = this.replacer.replace(text, target, replace, firstId); return (result == text) ? this.text : Unsafe.stringOf(result); } } }
public String with(String replacement) { if (null != pattern) { return pattern.matcher(text).replaceAll(replacement); } else { if (text.length() < keyword.length()) { return text; } int firstId = this.text.indexOf(this.keyword); if (firstId < 0) { return this.text; } char[] text = Unsafe.bufOf(this.text); char[] target = this.keyword.toCharArray(); char[] replace = replacement.toCharArray(); char[] result = this.replacer.replace(text, target, replace, firstId); return (result == text) ? this.text : Unsafe.stringOf(result); } } }
public RequestHandler findStaticGetHandler(String url) { Iterator<String> path = Path.tokenizer(Unsafe.bufOf(url)); Node node = root(H.Method.GET); while (null != node && path.hasNext()) { String nodeName = path.next(); node = node.staticChildren.get(nodeName); if (null == node || node.terminateRouteSearch()) { break; } } return null == node ? null : node.handler; }
public String in(String text) { if (null != replacementFunction) { E.illegalStateIf(null != pattern, "Replace with function doesnot support regex search"); replacement = replacementFunction.apply(keyword); } if (null != pattern) { return pattern.matcher(text).replaceAll(replacement); } if (text.length() < keyword.length()) { return text; } int firstId = text.indexOf(this.keyword); if (firstId < 0) { return text; } char[] textArray = Unsafe.bufOf(text); char[] target = this.keyword.toCharArray(); char[] replace = replacement.toCharArray(); char[] result = this.replacer.replace(textArray, target, replace, firstId); return (result == textArray) ? text : Unsafe.stringOf(result); } }
private Node _search(H.Method method, String path) { Node node = root(method); assert node != null; E.unsupportedIf(null == node, "Method %s is not supported", method); if (path.length() == 1 && path.charAt(0) == '/') { return node; } String sUrl = path; List<String> paths = Path.tokenize(Unsafe.bufOf(sUrl)); int len = paths.size(); for (int i = 0; i < len - 1; ++i) { node = node.findChild(paths.get(i)); if (null == node) return null; } return node.findChild(paths.get(len - 1)); }
public String in(String text) { if (null != replacementFunction) { E.illegalStateIf(null != pattern, "Replace with function doesnot support regex search"); replacement = replacementFunction.apply(keyword); } if (null != pattern) { return pattern.matcher(text).replaceAll(replacement); } if (text.length() < keyword.length()) { return text; } int firstId = text.indexOf(this.keyword); if (firstId < 0) { return text; } char[] textArray = Unsafe.bufOf(text); char[] target = this.keyword.toCharArray(); char[] replace = replacement.toCharArray(); char[] result = this.replacer.replace(textArray, target, replace, firstId); return (result == textArray) ? text : Unsafe.stringOf(result); } }
private Node _search(H.Method method, String path) { Node node = root(method); assert node != null; E.unsupportedIf(null == node, "Method %s is not supported", method); if (path.length() == 1 && path.charAt(0) == '/') { return node; } String sUrl = path; List<String> paths = Path.tokenize(Unsafe.bufOf(sUrl)); int len = paths.size(); for (int i = 0; i < len - 1; ++i) { node = node.findChild(paths.get(i)); if (null == node) return null; } return node.findChild(paths.get(len - 1)); }
public boolean startsWith(CharSequence suffix, int toffset) { if (suffix.length() == 0) return true; int sz2 = suffix.length(), sz = size(); if (toffset < 0 || toffset > sz - sz2) { return false; } int po = 0, pc = sz2, to = toffset; char[] buf1 = buf; try { char[] buf2 = Unsafe.bufOf(suffix); while (--pc >= 0) { if (buf1[toInternalId(to++)] != buf2[po++]) { return false; } } return true; } catch (RuntimeException e) { while (--pc >= 0) { if (buf1[toInternalId(to++)] != suffix.charAt(po++)) { return false; } } return true; } }
public boolean startsWith(CharSequence suffix, int toffset) { if (suffix.length() == 0) return true; int sz2 = suffix.length(), sz = size(); if (toffset < 0 || toffset > sz - sz2) { return false; } int po = 0, pc = sz2, to = toffset; char[] buf1 = buf; try { char[] buf2 = Unsafe.bufOf(suffix); while (--pc >= 0) { if (buf1[toInternalId(to++)] != buf2[po++]) { return false; } } return true; } catch (RuntimeException e) { while (--pc >= 0) { if (buf1[toInternalId(to++)] != suffix.charAt(po++)) { return false; } } return true; } }
public static String unsafeCapFirst(String s) { if (null == s) { return ""; } try { char[] buf = Unsafe.bufOf(s); char[] newBuf = unsafeCapFirst(buf, 0, buf.length); if (newBuf == buf) return s; return Unsafe.stringOf(newBuf); } catch (Exception e) { return capFirst(s); } }
public static String unsafeCapFirst(String s) { if (null == s) { return ""; } try { char[] buf = Unsafe.bufOf(s); char[] newBuf = unsafeCapFirst(buf, 0, buf.length); if (newBuf == buf) return s; return Unsafe.stringOf(newBuf); } catch (Exception e) { return capFirst(s); } }
public FastStr append(String s) { int sz = size(), sz2 = s.length(); if (0 == sz) return of(s); if (0 == sz2) return this; int newSz = sz + sz2; char[] newBuf = new char[newSz]; copyTo(newBuf, 0); boolean done = false; if (sz2 > 512) { try { char[] sBuf = Unsafe.bufOf(s); System.arraycopy(sBuf, 0, newBuf, sz, sz2); done = true; } catch (RuntimeException e) { // ignore } } if (!done) { for (int i = 0; i < sz2; ++i) { newBuf[sz + i] = s.charAt(i); } } return new FastStr(newBuf, 0, newSz); }
public FastStr append(String s) { int sz = size(), sz2 = s.length(); if (0 == sz) return of(s); if (0 == sz2) return this; int newSz = sz + sz2; char[] newBuf = new char[newSz]; copyTo(newBuf, 0); boolean done = false; if (sz2 > 512) { try { char[] sBuf = Unsafe.bufOf(s); System.arraycopy(sBuf, 0, newBuf, sz, sz2); done = true; } catch (RuntimeException e) { // ignore } } if (!done) { for (int i = 0; i < sz2; ++i) { newBuf[sz + i] = s.charAt(i); } } return new FastStr(newBuf, 0, newSz); }
private Node _locate(final H.Method method, final String path, String action) { Node node = root(method); E.unsupportedIf(null == node, "Method %s is not supported", method); assert null != node; int pathLen = path.length(); if (0 == pathLen || (1 == pathLen && path.charAt(0) == '/')) { return node; } String sUrl = path; List<String> paths = Path.tokenize(Unsafe.bufOf(sUrl)); int len = paths.size(); for (int i = 0; i < len - 1; ++i) { String part = paths.get(i); if (checkIgnoreRestParts(node, part)) { return node; } node = node.addChild(part, path, action); } String part = paths.get(len - 1); if (checkIgnoreRestParts(node, part)) { return node; } return node.addChild(part, path, action); }
private Node _locate(final H.Method method, final String path, String action) { Node node = root(method); E.unsupportedIf(null == node, "Method %s is not supported", method); assert null != node; int pathLen = path.length(); if (0 == pathLen || (1 == pathLen && path.charAt(0) == '/')) { return node; } String sUrl = path; List<String> paths = Path.tokenize(Unsafe.bufOf(sUrl)); int len = paths.size(); for (int i = 0; i < len - 1; ++i) { String part = paths.get(i); if (checkIgnoreRestParts(node, part)) { return node; } node = node.addChild(part, path, action); } String part = paths.get(len - 1); if (checkIgnoreRestParts(node, part)) { return node; } return node.addChild(part, path, action); }
public RequestHandler getInvoker(H.Method method, String path, ActionContext context) { context.router(this); if (method == H.Method.OPTIONS) { return optionHandlerFactory.optionHandler(path, context); } Node node = root(method, false); if (null == node) { return UnknownHttpMethodHandler.INSTANCE; } node = search(node, Path.tokenizer(Unsafe.bufOf(path)), context); RequestHandler handler = getInvokerFrom(node); RequestHandler blockIssueHandler = app().blockIssueHandler(); if (null == blockIssueHandler || (handler instanceof FileGetter || handler instanceof ResourceGetter)) { if (null != node) { context.routeSource(node.routeSource); } // otherwise handler is always 404 return handler; } return blockIssueHandler; }
public RequestHandler getInvoker(H.Method method, String path, ActionContext context) { context.router(this); if (method == H.Method.OPTIONS) { return optionHandlerFactory.optionHandler(path, context); } Node node = root(method, false); if (null == node) { return UnknownHttpMethodHandler.INSTANCE; } node = search(node, Path.tokenizer(Unsafe.bufOf(path)), context); RequestHandler handler = getInvokerFrom(node); RequestHandler blockIssueHandler = app().blockIssueHandler(); if (null == blockIssueHandler || (handler instanceof FileGetter || handler instanceof ResourceGetter)) { if (null != node) { context.routeSource(node.routeSource); } // otherwise handler is always 404 return handler; } return blockIssueHandler; }