0001: /*
0002: * $Id: AnyString.java,v 1.75 2002/09/16 08:05:02 jkl Exp $
0003: *
0004: * Copyright (c) 2002 Njet Communications Ltd. All Rights Reserved.
0005: *
0006: * Use is subject to license terms, as defined in
0007: * Anvil Sofware License, Version 1.1. See LICENSE
0008: * file, or http://njet.org/license-1.1.txt
0009: */
0010: package anvil.core;
0011:
0012: import anvil.core.runtime.AnyFunction;
0013: import anvil.script.Context;
0014: import anvil.script.Function;
0015: import anvil.java.util.BindingEnumeration;
0016: import anvil.util.Conversions;
0017: import java.io.IOException;
0018: import java.io.OutputStream;
0019: import java.io.Writer;
0020: import java.util.StringTokenizer;
0021: import java.util.Vector;
0022: import java.util.Comparator;
0023: import org.apache.oro.text.regex.MatchResult;
0024: import org.apache.oro.text.regex.Pattern;
0025: import org.apache.oro.text.regex.PatternMatcherInput;
0026: import org.apache.oro.text.regex.Perl5Compiler;
0027: import org.apache.oro.text.regex.Perl5Matcher;
0028: import org.apache.oro.text.regex.Perl5Substitution;
0029: import org.apache.oro.text.regex.StringSubstitution;
0030: import org.apache.oro.text.regex.Substitution;
0031: import org.apache.oro.text.regex.Util;
0032:
0033: ///
0034: /// @class string
0035: /// The <code>string</code> class represents character strings.
0036: /// String is a sequence of characters.
0037:
0038: /**
0039: * class <code>AnyString</code>
0040: *
0041: * @author Jani Lehtimäki
0042: */
0043: public class AnyString extends AnySequence {
0044:
0045: /// @constructor string
0046: /// Creates new string from the string representation of given parameter.
0047: /// @synopsis string(object value)
0048: public static final Object[] newInstance = { "value" };
0049:
0050: public static final Any newInstance(Any value) {
0051: return value.toAnyString();
0052: }
0053:
0054: protected String _string;
0055: protected int _hashcode = 0;
0056: protected int _type = IS_NULL;
0057: protected long _long = 0;
0058: protected double _double = 0.0;
0059:
0060: public AnyString(boolean bool) {
0061: _string = bool ? "true" : "false";
0062: }
0063:
0064: public AnyString(int integer) {
0065: _string = String.valueOf(integer);
0066: }
0067:
0068: public AnyString(double dbl) {
0069: _string = String.valueOf(dbl);
0070: }
0071:
0072: public AnyString(String string) {
0073: if (string == null) {
0074: _string = "";
0075: } else {
0076: _string = string;
0077: }
0078: }
0079:
0080: public boolean isCompatible(Any element) {
0081: int t = element.typeOf();
0082: return (t == IS_STRING) || (t == IS_BUFFER) || (t == IS_BINARY);
0083: }
0084:
0085: public int getSize() {
0086: return _string.length();
0087: }
0088:
0089: public AnySequence setSize(int size) {
0090: int length = _string.length();
0091: if (size < length) {
0092: return new AnyString(_string.substring(0, size));
0093: } else if (size > length) {
0094: StringBuffer buffer = new StringBuffer(size);
0095: buffer.append(_string);
0096: size -= length;
0097: while (size-- > 0) {
0098: buffer.append(0);
0099: }
0100: return new AnyString(buffer.toString());
0101: } else {
0102: return this ;
0103: }
0104: }
0105:
0106: public AnySequence clear() {
0107: return new AnyString("");
0108: }
0109:
0110: public Any getElement(int index) {
0111: return ObjectPool.createChar(_string.charAt(index));
0112: }
0113:
0114: public AnySequence setElement(int index, Any element) {
0115: StringBuffer buffer = new StringBuffer(_string);
0116: buffer.setCharAt(index, element.toChar());
0117: return new AnyString(buffer.toString());
0118: }
0119:
0120: public AnySequence crop(int start, int length) {
0121: return new AnyString(_string.substring(start, start + length));
0122: }
0123:
0124: public AnySequence getSlice(int start, int length) {
0125: return new AnyString(_string.substring(start, start + length));
0126: }
0127:
0128: public AnySequence deleteSlice(int start, int length) {
0129: String string = _string;
0130: int size = string.length();
0131: StringBuffer buffer = new StringBuffer(size - length);
0132: if (start > 0) {
0133: buffer.append(string.substring(0, start));
0134: }
0135: if (start + length < size) {
0136: buffer.append(string.substring(start + length, size));
0137: }
0138: return new AnyString(buffer.toString());
0139: }
0140:
0141: public AnySequence setSlice(int start, int length, Any element) {
0142: String string = _string;
0143: int size = string.length();
0144: char seq = element.toChar();
0145: StringBuffer buffer = new StringBuffer(size + 1 - length);
0146: if (start > 0) {
0147: buffer.append(string.substring(0, start));
0148: }
0149: buffer.append(seq);
0150: if (start + length < size) {
0151: buffer.append(string.substring(start + length, size));
0152: }
0153: return new AnyString(buffer.toString());
0154: }
0155:
0156: public AnySequence setSlice(int start, int length,
0157: AnySequence sequence) {
0158: String string = _string;
0159: int size = string.length();
0160: String seq = sequence.toString();
0161: int seq_length = seq.length();
0162: StringBuffer buffer = new StringBuffer(size + seq_length
0163: - length);
0164: if (start > 0) {
0165: buffer.append(string.substring(0, start));
0166: }
0167: if (seq_length > 0) {
0168: buffer.append(seq);
0169: }
0170: if (start + length < size) {
0171: buffer.append(string.substring(start + length, size));
0172: }
0173: return new AnyString(buffer.toString());
0174:
0175: }
0176:
0177: public AnySequence append(AnySequence sequence) {
0178: return new AnyString(_string + sequence.toString());
0179: }
0180:
0181: public AnySequence append(Any element) {
0182: return new AnyString(_string + element.toString());
0183: }
0184:
0185: public AnySequence createSequence(Any element) {
0186: return new AnyString(element.toString());
0187: }
0188:
0189: public AnySequence createEmptySequence() {
0190: return new AnyString("");
0191: }
0192:
0193: public int compareAt(AnySequence sequence, int start, int length) {
0194: String haystack = _string;
0195: String needle = sequence.toString();
0196: int delta;
0197: for (int i = 0; i < length; i++) {
0198: delta = haystack.charAt(start + i) - needle.charAt(i);
0199: if (delta != 0) {
0200: return (delta < 0) ? -1 : ((delta > 0) ? 1 : 0);
0201: }
0202: }
0203: return 0;
0204: }
0205:
0206: public int compareAt(Any element, int start) {
0207: int delta = _string.charAt(start) - element.toChar();
0208: return (delta < 0) ? -1 : ((delta > 0) ? 1 : 0);
0209: }
0210:
0211: public AnySequence fill(Any fill, int start, int length) {
0212: char[] array = _string.toCharArray();
0213: java.util.Arrays.fill(array, start, start + length, fill
0214: .toChar());
0215: return new AnyString(new String(array));
0216: }
0217:
0218: public AnySequence sort(int start, int length, Comparator comparator) {
0219: char[] array = _string.toCharArray();
0220: java.util.Arrays.sort(array, start, start + length);
0221: return new AnyString(new String(array));
0222: }
0223:
0224: public int search(Any element, Comparator comparator) {
0225: return java.util.Arrays.binarySearch(_string.toCharArray(),
0226: element.toChar());
0227: }
0228:
0229: public AnySequence swap(int index1, int index2) {
0230: char[] array = _string.toCharArray();
0231: char ch = array[index1];
0232: array[index1] = array[index2];
0233: array[index2] = ch;
0234: return new AnyString(new String(array));
0235: }
0236:
0237: public AnySequence reverse() {
0238: String string = _string;
0239: int size = string.length();
0240: StringBuffer buffer = new StringBuffer(size);
0241: for (int p = size - 1; p >= 0; p--) {
0242: buffer.append(string.charAt(p));
0243: }
0244: return new AnyString(buffer.toString());
0245: }
0246:
0247: public int typeOf() {
0248: return IS_STRING;
0249: }
0250:
0251: public int sizeOf() {
0252: return _string.length();
0253: }
0254:
0255: public boolean isMutable() {
0256: return false;
0257: }
0258:
0259: public boolean isString() {
0260: return true;
0261: }
0262:
0263: public final anvil.script.ClassType classOf() {
0264: return __class__;
0265: }
0266:
0267: public boolean toBoolean() {
0268: return _string.length() > 0;
0269: }
0270:
0271: private void parse() {
0272: int n = _string.length();
0273: if (n == 0) {
0274: return;
0275: }
0276:
0277: char ch = 0;
0278: int i = 0;
0279:
0280: if (n > 0) {
0281: ch = _string.charAt(0);
0282: if (ch == '-' || ch == '+') {
0283: i++;
0284: }
0285: }
0286: while (i < n && Character.isDigit((ch = _string.charAt(i)))) {
0287: i++;
0288: }
0289:
0290: _type = IS_STRING;
0291:
0292: if (i < n) {
0293: if (ch == '.' || ch == 'e' || ch == 'E') {
0294: try {
0295: _double = Double.parseDouble(_string);
0296: _long = (long) _double;
0297: _type = IS_DOUBLE;
0298: } catch (NumberFormatException e) {
0299: }
0300: }
0301: } else {
0302: try {
0303: _long = Long.parseLong(_string);
0304: _double = (double) _long;
0305: _type = IS_INT;
0306: } catch (NumberFormatException e) {
0307: }
0308: }
0309:
0310: }
0311:
0312: public Any coerce() {
0313: if (_type == IS_NULL) {
0314: parse();
0315: }
0316: switch (_type) {
0317: case IS_INT:
0318: return Any.create(_long);
0319: case IS_DOUBLE:
0320: return Any.create(_double);
0321: case IS_STRING:
0322: default:
0323: return this ;
0324: }
0325: }
0326:
0327: public int toInt() {
0328: if (_type == IS_NULL) {
0329: parse();
0330: }
0331: return (int) _long;
0332: }
0333:
0334: public long toLong() {
0335: if (_type == IS_NULL) {
0336: parse();
0337: }
0338: return _long;
0339: }
0340:
0341: public double toDouble() {
0342: if (_type == IS_NULL) {
0343: parse();
0344: }
0345: return _double;
0346: }
0347:
0348: public char toChar() {
0349: return (_string.length() > 0) ? _string.charAt(0) : ' ';
0350: }
0351:
0352: public String toString() {
0353: return _string;
0354: }
0355:
0356: public byte[] toBinary() {
0357: return anvil.util.Conversions.getBytes(_string);
0358: }
0359:
0360: public Writer toAnvil(Writer writer) throws IOException {
0361: writer.write('"');
0362: writer.write(anvil.util.Conversions.escape(_string, true));
0363: writer.write('"');
0364: return writer;
0365: }
0366:
0367: public Writer toJava(Writer writer) throws IOException {
0368: writer.write("anvil.core.Any.create(\"");
0369: writer.write(anvil.util.Conversions.escape(_string, false));
0370: writer.write('"');
0371: writer.write(')');
0372: return writer;
0373: }
0374:
0375: public anvil.codec.Code toCode(anvil.codec.Code code) {
0376: anvil.codec.ConstantPool pool = code.getPool();
0377: int clazz = pool.addClass("anvil/core/AnyString");
0378: code.anew(clazz);
0379: code.dup();
0380: code.astring(_string);
0381: code.invokespecial(pool.addMethodRef(clazz, "<init>",
0382: "(Ljava/lang/String;)V"));
0383: return code;
0384: }
0385:
0386: public Object toObject() {
0387: return _string;
0388: }
0389:
0390: public Any toAnyString() {
0391: return this ;
0392: }
0393:
0394: public int hashCode() {
0395: if (_type == IS_NULL) {
0396: parse();
0397: }
0398: switch (_type) {
0399: case IS_INT:
0400: return (int) _long;
0401: case IS_DOUBLE:
0402: return (int) _double;
0403: default:
0404: if (_hashcode == 0) {
0405: _hashcode = _string.hashCode();
0406: }
0407: return _hashcode;
0408: }
0409: }
0410:
0411: public Any increase() {
0412: if (_type == IS_NULL) {
0413: parse();
0414: }
0415: switch (_type) {
0416: case IS_INT:
0417: return Any.create(_long + 1);
0418: case IS_DOUBLE:
0419: return Any.create(_double + 1.0);
0420: default:
0421: return this ;
0422: }
0423: }
0424:
0425: public Any decrease() {
0426: if (_type == IS_NULL) {
0427: parse();
0428: }
0429: switch (_type) {
0430: case IS_INT:
0431: return Any.create(_long - 1);
0432: case IS_DOUBLE:
0433: return Any.create(_double - 1.0);
0434: default:
0435: return this ;
0436: }
0437: }
0438:
0439: public Any minus() {
0440: if (_type == IS_NULL) {
0441: parse();
0442: }
0443: switch (_type) {
0444: case IS_INT:
0445: return Any.create(-_long);
0446: case IS_DOUBLE:
0447: return Any.create(-_double);
0448: default:
0449: return this ;
0450: }
0451: }
0452:
0453: public Any plus() {
0454: if (_type == IS_NULL) {
0455: parse();
0456: }
0457: switch (_type) {
0458: case IS_INT:
0459: return Any.create(_long);
0460: case IS_DOUBLE:
0461: return Any.create(_double);
0462: default:
0463: return this ;
0464: }
0465: }
0466:
0467: public Any add(Any other) {
0468: return Any.create(_string + other.toString());
0469: }
0470:
0471: public boolean equals(Object obj) {
0472: if (this == obj) {
0473: return true;
0474: }
0475: if (obj instanceof AnyString) {
0476: return _string.equals(obj.toString());
0477: }
0478: return false;
0479: }
0480:
0481: protected int compare(Any other) {
0482: return _string.compareTo(other.toString());
0483: }
0484:
0485: public void serialize(Serializer serializer) throws IOException {
0486: if (serializer.register(this )) {
0487: return;
0488: }
0489: String string = _string;
0490: serializer.write('s');
0491: serializer.write(string.length());
0492: serializer.write(':');
0493: serializer.writeUTF16(string);
0494: }
0495:
0496: public BindingEnumeration enumeration() {
0497: return new StringEnumeration(_string);
0498: }
0499:
0500: /********* Exposed methods ************/
0501:
0502: /// @method length
0503: /// Returns the length of this string.
0504: /// @synopsis int length()
0505: public Any m_length() {
0506: return Any.create(_string.length());
0507: }
0508:
0509: /// @method unquote
0510: /// Unquotes all html entities from this string.
0511: /// @synopsis string unquote()
0512: public Any m_unquote() {
0513: return Any.create(Conversions.decodeEntities(_string));
0514: }
0515:
0516: /// @method quote
0517: /// Converts all applicable characters with corresponding
0518: /// html entity.
0519: /// @synopsis string quote()
0520: public Any m_quote() {
0521: return Any.create(Conversions.encodeEntities(_string));
0522: }
0523:
0524: /// @method quoteMeta
0525: /// Converts all applicable characters under >127 with corresponding
0526: /// html entity.
0527: /// @synopsis string quoteMeta()
0528: public Any m_quoteMeta() {
0529: return Any.create(Conversions.encodeMetaEntities(_string));
0530: }
0531:
0532: /// @method quoteText
0533: /// Converts all applicable characters over >127 with corresponding
0534: /// html entity.
0535: /// @synopsis string quoteText()
0536: public Any m_quoteText() {
0537: return Any.create(Conversions.encodeText(_string));
0538: }
0539:
0540: /// @method compress
0541: /// Compresses consequtive white space characeters into single
0542: /// space.
0543: /// @synopsis string compress()
0544: public Any m_compress() {
0545: return Any.create(Conversions.compress(_string));
0546: }
0547:
0548: /// @method capitalize
0549: /// Capitalizes the first character of all words.
0550: /// @synopsis string capitalize()
0551: public Any m_capitalize() {
0552: return Any.create(Conversions.capitalizeAllWords(_string));
0553: }
0554:
0555: /// @method capitalizeFirst
0556: /// Capitalizes first character of first word.
0557: /// @synopsis string capitalizeFirst()
0558: public Any m_capitalizeFirst() {
0559: return Any.create(Conversions.capitalizeFirstWord(_string));
0560: }
0561:
0562: /// @method nowrap
0563: /// Converts white spaces into <code>&nbsp;</code>.
0564: /// @synopsis string nowrap()
0565: public Any m_nowrap() {
0566: return Any.create(Conversions.nowrap(_string));
0567: }
0568:
0569: /// @method encodeURL
0570: /// URL encodes this string.
0571: /// @synopsis string encodeURL()
0572: public Any m_encodeURL() {
0573: return Any.create(Conversions.URLEncode(_string));
0574: }
0575:
0576: /// @method decodeURL
0577: /// Decodes URL encoding from this string.
0578: /// @synopsis string decodeURL()
0579: public Any m_decodeURL() {
0580: return Any.create(Conversions.URLDecode(_string));
0581: }
0582:
0583: /// @method encode
0584: /// URL encodes this string.
0585: /// @synopsis string encode()
0586: public Any m_encode() {
0587: return Any.create(Conversions.URLEncode(_string));
0588: }
0589:
0590: /// @method decode
0591: /// Decodes URL encoding from this string.
0592: /// @synopsis string decode()
0593: public Any m_decode() {
0594: return Any.create(Conversions.URLDecode(_string));
0595: }
0596:
0597: /// @method escape
0598: /// Escapes string using the rules of anvil string literal.
0599: /// @synopsis string escape()
0600: public Any m_escape() {
0601: return Any.create(Conversions.escape(_string));
0602: }
0603:
0604: /// @method unescape
0605: /// Unescapes backslash quotes.
0606: /// @synopsis string unescape()
0607: /// @synopsis string unescape(boolean withQuotes)
0608: public static final Object[] p_unescape = new Object[] {
0609: "*hasQuotes", Boolean.FALSE };
0610:
0611: public Any m_unescape(boolean hasQuotes) {
0612: return Any.create(Conversions.unescape(_string, hasQuotes));
0613: }
0614:
0615: /// @method nl2br
0616: /// Convers all line feeds into <code><br>\n</code>
0617: /// @synopsis string nl2br()
0618: public Any m_nl2br() {
0619: return Any.create(Conversions.nl2br(_string));
0620: }
0621:
0622: /// @method toUpper
0623: /// Converts all lower case characeters into corresponding upper case
0624: /// characeters.
0625: /// @synopsis string toUpper()
0626: public Any m_toUpper() {
0627: return Any.create(_string.toUpperCase());
0628: }
0629:
0630: /// @method toLower
0631: /// Converts all upper case characeters into corresponding lower case
0632: /// characeters.
0633: /// @synopsis string toLower()
0634: public Any m_toLower() {
0635: return Any.create(_string.toLowerCase());
0636: }
0637:
0638: /// @method trim
0639: /// Removes whitespace characters from the beginning and end of this string.
0640: /// @synopsis string trim()
0641: public Any m_trim() {
0642: return Any.create(_string.trim());
0643: }
0644:
0645: /// @method trimLeft
0646: /// Removes whitespace characters from the beginning this string.
0647: /// @synopsis string trimLeft()
0648: public Any m_trimLeft() {
0649: String value = _string;
0650: int i = 0;
0651: int n = value.length();
0652: while ((i < n) && Character.isWhitespace(value.charAt(i))) {
0653: i++;
0654: }
0655: if (i > 0) {
0656: return Any.create(value.substring(i));
0657: } else {
0658: return this ;
0659: }
0660: }
0661:
0662: /// @method trimRight
0663: /// Removes whitespace characters from the end this string.
0664: /// @synopsis string trimRight()
0665: public Any m_trimRight() {
0666: String value = _string;
0667: int i = value.length();
0668: while ((i > 0) && Character.isWhitespace(value.charAt(i - 1))) {
0669: i--;
0670: }
0671: if (i < value.length()) {
0672: return Any.create(value.substring(0, i));
0673: } else {
0674: return this ;
0675: }
0676: }
0677:
0678: /// @method substring
0679: /// Returns a substring from the given region of this string.
0680: /// @synopsis string substring(int start)
0681: /// @synopsis string substring(int start, int end)
0682: /// @param start Starting index, included
0683: /// @param end Ending index, excluded
0684: public static final Object[] p_substring = new Object[] { "start",
0685: "*end", null };
0686:
0687: public Any m_substring(int start, Any end_) {
0688: String string = _string;
0689: int max = string.length();
0690:
0691: if (start < 0) {
0692: start = 0;
0693: } else if (start > max) {
0694: start = max;
0695: }
0696:
0697: if (end_ != null) {
0698: int end = end_.toInt();
0699: if (end < 0) {
0700: end = 0;
0701: } else if (end > max) {
0702: end = max;
0703: }
0704: if (start <= end) {
0705: if (end - start == 1) {
0706: return ObjectPool.createChar(string.charAt(start));
0707: } else {
0708: return Any.create(string.substring(start, end));
0709: }
0710: } else {
0711: return EMPTY_STRING;
0712: }
0713: } else {
0714: if (start == max) {
0715: return EMPTY_STRING;
0716: } else {
0717: return Any.create(string.substring(start));
0718: }
0719: }
0720: }
0721:
0722: /// @method charAt
0723: /// Returns single character from this string.
0724: /// @synopsis string charAt(int index)
0725: /// @param index Index to string
0726: public static final Object[] p_charAt = new Object[] { "index" };
0727:
0728: public Any m_charAt(int index) {
0729: if ((index >= 0) && (index < _string.length())) {
0730: return Any.create(_string.charAt(index));
0731: } else {
0732: return EMPTY_STRING;
0733: }
0734: }
0735:
0736: /// @method charCodeAt
0737: /// Returns the character code at the speicified index.
0738: /// @synopsis int charCodeAt(int index)
0739: /// @param index Index to string
0740: public static final Object[] p_charCodeAt = new Object[] { "index" };
0741:
0742: public Any m_charCodeAt(int index) {
0743: if ((index >= 0) && (index < _string.length())) {
0744: return Any.create((int) _string.charAt(index));
0745: } else {
0746: return ZERO;
0747: }
0748: }
0749:
0750: /// @method indexOf
0751: /// Returns the index within this string of the first occurrence of the
0752: /// specified string.
0753: /// @synopsis int indexOf(string needle)
0754: /// @synopsis int indexOf(string needle, int fromIndex)
0755: /// @param needle String to search for
0756: /// @param fromIndex Starting index of search
0757: /// @return The index of first occurence, or -1 if it doesn't occur.
0758: public static final Object[] p_indexOf = new Object[] { "needle",
0759: "*fromIndex", new Integer(0) };
0760:
0761: public Any m_indexOf(String needle, int fromIndex) {
0762: return Any.create(_string.indexOf(needle, fromIndex));
0763: }
0764:
0765: /// @method lastIndexOf
0766: /// Returns the index within this string of the last occurrence of the
0767: /// specified string.
0768: /// @synopsis int lastIndexOf(string needle)
0769: /// @synopsis int lastIndexOf(string needle, int fromIndex)
0770: /// @param needle String to search for
0771: /// @param fromIndex Starting index of search
0772: /// @return The index of last occurence, or -1 if it doesn't occur.
0773: public static final Object[] p_lastIndexOf = new Object[] {
0774: "needle", "*fromIndex", null };
0775:
0776: public Any m_lastIndexOf(String needle, Any fromIndex) {
0777: int start;
0778: if (fromIndex != null) {
0779: start = fromIndex.toInt();
0780: } else {
0781: start = _string.length();
0782: }
0783: return Any.create(_string.lastIndexOf(needle, start));
0784: }
0785:
0786: /// @method startsWith
0787: /// Checks if this string starts with given prefix.
0788: /// @synopsis boolean startsWith(string prefix)
0789: public static final Object[] p_startsWith = new Object[] { "prefix" };
0790:
0791: public Any m_startsWith(String prefix) {
0792: return _string.startsWith(prefix) ? TRUE : FALSE;
0793: }
0794:
0795: /// @method endsWith
0796: /// Checks if this string ends with given suffix.
0797: /// @synopsis boolean endsWith(string suffix)
0798: public static final Object[] p_endsWith = new Object[] { "suffix" };
0799:
0800: public Any m_endsWith(String suffix) {
0801: return _string.endsWith(suffix) ? TRUE : FALSE;
0802: }
0803:
0804: /// @method compareToIgnoreCase
0805: /// Compares this string and given string, lower- and
0806: /// uppercase characters are considered equal.
0807: /// @synopsis int compareToIgnoreCase(string other)
0808: /// @return -1 if <code>this<other</code>,
0809: /// 0 if <code>this==other</code>,
0810: /// 1 if <code>this>other</code>,
0811: public static final Object[] p_compareToIgnoreCase = new Object[] { "str" };
0812:
0813: public Any m_compareToIgnoreCase(String str) {
0814: int delta = _string.compareToIgnoreCase(str);
0815: if (delta < 0) {
0816: return MINUS_ONE;
0817: } else if (delta > 0) {
0818: return ONE;
0819: } else {
0820: return ZERO;
0821: }
0822: }
0823:
0824: /// @method equalsIgnoreCase
0825: /// Checks if this string is equal to given string, lower- and
0826: /// uppercase characters are considered equal
0827: /// @synopsis boolean equalsIgnoreCase(string other)
0828: /// @param other String to compare with
0829: public static final Object[] p_equalsIgnoreCase = new Object[] { "str" };
0830:
0831: public Any m_equalsIgnoreCase(String str) {
0832: return _string.equalsIgnoreCase(str) ? TRUE : FALSE;
0833: }
0834:
0835: /// @method explode
0836: /// Splits this string into regions using given set of delimiter
0837: /// characters.
0838: /// @synopsis list explode(string delimiters)
0839: /// @synopsis list explode(string delimiters, boolean returnEmpty)
0840: /// @param delimiters Set of delimiter characters
0841: /// @param returnEmpty If <code>true</code> also empty regions are returned, if
0842: /// omitted or <code>false</code> empty regions are not returned.
0843: public static final Object[] p_explode = new Object[] {
0844: "delimiters", "*returnEmpty", Boolean.FALSE };
0845:
0846: public Any m_explode(String delimiters, boolean returnEmpty) {
0847: AnyList list = new AnyList(new Any[8], 0);
0848: if (returnEmpty) {
0849: String str = _string;
0850: int n = str.length();
0851: int begin = 0;
0852: for (int i = 0; i < n; i++) {
0853: if (delimiters.indexOf(str.charAt(i)) >= 0) {
0854: list.append(Any.create(str.substring(begin, i)));
0855: begin = i + 1;
0856: }
0857: }
0858: if (begin <= n) {
0859: list.append(Any.create(str.substring(begin)));
0860: }
0861: } else {
0862: StringTokenizer tokenizer = new StringTokenizer(_string,
0863: delimiters);
0864: while (tokenizer.hasMoreElements()) {
0865: list.append(Any
0866: .create((String) tokenizer.nextElement()));
0867: }
0868: }
0869: return list;
0870: }
0871:
0872: /// @method repeat
0873: /// Repeats this string <code>amount</code> times.
0874: /// @synopsis string repeat(int amount)
0875: public static final Object[] p_repeat = new Object[] { "count" };
0876:
0877: public Any m_repeat(int count) {
0878: if (count < 0) {
0879: count = 0;
0880: }
0881: String string = _string;
0882: StringBuffer buffer = new StringBuffer(string.length() * count
0883: + 1);
0884: for (; count > 0; count--) {
0885: buffer.append(string);
0886: }
0887: return Any.create(buffer.toString());
0888: }
0889:
0890: /// @method translate
0891: /// For each character in this string: if character appears
0892: /// in <code>from</code> string, converts it to character
0893: /// at the same index at <code>to</code>.
0894: /// @synopsis string translate(string from, string to)
0895: /// @param from Source character mapping
0896: /// @param to Target character mapping
0897: /// @return Translated string
0898: public static final Object[] p_translate = new Object[] { "from",
0899: "to" };
0900:
0901: public Any m_translate(String from, String to) {
0902: int pos;
0903: char ch;
0904: String string = _string;
0905: int n = string.length();
0906: int max = to.length();
0907: StringBuffer buffer = new StringBuffer(n);
0908: for (int i = 0; i < n; i++) {
0909: ch = string.charAt(i);
0910: pos = from.indexOf(ch);
0911: if (pos >= 0) {
0912: if (pos < max) {
0913: buffer.append(to.charAt(pos));
0914: }
0915: } else {
0916: buffer.append(ch);
0917: }
0918: }
0919: return Any.create(buffer.toString());
0920: }
0921:
0922: /// @method replace
0923: /// Replaces all occurences of <code>from</code> into <code>to</code>.
0924: /// @synopsis string replace(string from, string to)
0925: /// @param from Regions to replace
0926: /// @param to Replacement
0927: public static final Object[] p_replace = new Object[] { "from",
0928: "to" };
0929:
0930: public Any m_replace(String from, String to) {
0931: String string = _string;
0932: int end = string.indexOf(from, 0);
0933: if (end < 0) {
0934: return this ;
0935: }
0936: int max = string.length();
0937: int begin = 0;
0938: int toLen = to.length();
0939: int fromLen = from.length();
0940: StringBuffer buffer = new StringBuffer(max);
0941: for (;;) {
0942: end = string.indexOf(from, begin);
0943: if (end < 0) {
0944: end = max;
0945: for (; begin < end; begin++) {
0946: buffer.append(string.charAt(begin));
0947: }
0948: break;
0949: } else {
0950: for (; begin < end; begin++) {
0951: buffer.append(string.charAt(begin));
0952: }
0953: buffer.append(to);
0954: begin += fromLen;
0955: }
0956: }
0957: return Any.create(buffer.toString());
0958: }
0959:
0960: /// @method unserialize
0961: /// Unserializes data contained in this string.
0962: /// @synopsis object unserialize()
0963: /// @return Unserialized data
0964: /// @throws CorruptedSerialization If serialized data is corrupted
0965: public Any m_unserialize(Context context) {
0966: try {
0967: return Serialization.unserialize(context, _string);
0968: } catch (UnserializationException e) {
0969: throw context.CorruptedSerialization();
0970: }
0971: }
0972:
0973: private Any doMatch(Context context, Any pattern_, Any matches_,
0974: boolean isMatch) {
0975: Array array = null;
0976: AnyList list = null;
0977: if (matches_ != null) {
0978: array = matches_.toArray();
0979: if (matches_ instanceof AnyList) {
0980: list = (AnyList) matches_;
0981: }
0982: }
0983:
0984: Pattern pattern = ObjectPool.createPattern(context, pattern_);
0985: Perl5Matcher matcher = new Perl5Matcher();
0986: if (isMatch ? matcher.matches(_string, pattern) : matcher
0987: .contains(_string, pattern)) {
0988: if (array != null) {
0989: MatchResult match = matcher.getMatch();
0990: array.clear();
0991: int n = match.groups();
0992: for (int i = 0; i < n; i++) {
0993: array.append(Any.create(match.group(i)));
0994: }
0995: return array;
0996:
0997: } else if (list != null) {
0998: MatchResult match = matcher.getMatch();
0999: list.clear();
1000: int n = match.groups();
1001: for (int i = 0; i < n; i++) {
1002: list.append(Any.create(match.group(i)));
1003: }
1004: return list;
1005: }
1006:
1007: return TRUE;
1008: } else {
1009: return FALSE;
1010: }
1011: }
1012:
1013: /// @method matches
1014: /// Checks if this (whole) string matches given
1015: /// regular expression.
1016: /// @synopsis boolean matches(string pattern)
1017: /// @synopsis boolean matches(string pattern, array matches)
1018: /// @synopsis boolean matches(string pattern, list matches)
1019: /// @synopsis boolean matches(pattern pattern)
1020: /// @synopsis boolean matches(pattern pattern, array matches)
1021: /// @synopsis boolean matches(pattern pattern, list matches)
1022: /// @param pattern Regular expression pattern
1023: /// @return Matches or boolean
1024: /// @throws MalformedPattern If pattern is invalid
1025: public static final Object[] p_matches = new Object[] { null,
1026: "pattern", "*matches", null };
1027:
1028: public Any m_matches(Context context, Any pattern, Any matches) {
1029: return doMatch(context, pattern, matches, true);
1030: }
1031:
1032: /// @method contains
1033: /// Checks if some region of this string matches given
1034: /// regular expression.
1035: /// @synopsis boolean contains(string pattern)
1036: /// @synopsis boolean contains(string pattern, array matches)
1037: /// @synopsis boolean contains(string pattern, list matches)
1038: /// @synopsis boolean contains(pattern pattern)
1039: /// @synopsis boolean contains(pattern pattern, array matches)
1040: /// @synopsis boolean contains(pattern pattern, list matches)
1041: /// @param pattern Regular expression pattern
1042: /// @return Matches or boolean
1043: /// @throws MalformedPattern If pattern is invalid
1044: public static final Object[] p_contains = new Object[] { null,
1045: "pattern", "*matches", null };
1046:
1047: public Any m_contains(Context context, Any pattern, Any matches) {
1048: return doMatch(context, pattern, matches, false);
1049: }
1050:
1051: /// @method find
1052: /// Finds all regions from this string mathing given regular
1053: /// expression pattern.
1054: /// @synopsis enumeration find(string pattern)
1055: /// @synopsis enumeration find(pattern pattern)
1056: /// @param pattern Regular expression pattern
1057: /// @return Enumeration of strings
1058: /// @throws MalformedPattern If pattern is invalid
1059: public static final Object[] p_find = new Object[] { null,
1060: "pattern" };
1061:
1062: public Any m_find(Context context, Any pattern_) {
1063: Pattern pattern = ObjectPool.createPattern(context, pattern_);
1064: return new AnyBindingEnumeration(
1065: new AnyUtils.StringPatternMatcher(pattern, _string));
1066: }
1067:
1068: /// @method split
1069: /// Splits this string using given regular expression pattern as
1070: /// delimiter.
1071: /// @synopsis list split(string pattern)
1072: /// @synopsis list split(pattern pattern)
1073: /// @param pattern Regular expression pattern
1074: /// @throws MalformedPattern If pattern is invalid
1075: public static final Object[] p_split = new Object[] { null,
1076: "pattern" };
1077:
1078: public Any m_split(Context context, Any pattern_) {
1079: Pattern pattern = ObjectPool.createPattern(context, pattern_);
1080: Perl5Matcher matcher = new Perl5Matcher();
1081: Vector parts = Util.split(matcher, pattern, _string);
1082: int n = parts.size();
1083: Any[] list = new Any[n];
1084: for (int i = 0; i < n; i++) {
1085: list[i] = Any.create(parts.elementAt(i).toString());
1086: }
1087: return new AnyList(list);
1088: }
1089:
1090: /// @method divide
1091: /// Divides string to two pieces, using <code>delim</code> as delimiter.
1092: /// Example 1. "foo.bar.foo".divide(".") returns ("foo", "bar.foo")
1093: /// Example 2. "foo.bar.foo".divide("|") returns ("foo.bar.foo", null)
1094: /// Example 3. "foo.bar.fiu".divide(".", true) return ("fiu", "foo.bar")
1095: /// @synopsis list divide(string delim)
1096: /// @synopsis list divide(string delim, boolean dir)
1097: /// @param delim Delimiter character(s)
1098: /// @param dir Direction (default false), false = from start of the string,
1099: /// true = from end of the string
1100: public static final Object[] p_divide = new Object[] { "delim",
1101: "*dir", null };
1102:
1103: public Any m_divide(String delim, Any dir) {
1104: if (delim == null) {
1105: return UNDEFINED;
1106: }
1107: if (dir == null || !dir.toBoolean()) {
1108: int i = _string.indexOf(delim);
1109: if (i == -1) {
1110: return new AnyList(new Any[] { this , NULL });
1111: } else {
1112: return new AnyList(new Any[] {
1113: Any.create(_string.substring(0, i)),
1114: Any.create(_string.substring(i + 1)) });
1115: }
1116: } else {
1117: int i = _string.lastIndexOf(delim);
1118: if (i == -1) {
1119: return new AnyList(new Any[] { this , NULL });
1120: } else {
1121: return new AnyList(new Any[] {
1122: Any.create(_string.substring(i + 1)),
1123: Any.create(_string.substring(0, i)) });
1124: }
1125: }
1126: }
1127:
1128: private Any doSubstitute(Context context, Any pattern_,
1129: Any replacement, int flags) {
1130: Substitution substitution;
1131: if (replacement.isString()) {
1132: String s = replacement.toString();
1133: if (s.indexOf('$') != -1) {
1134: substitution = new Perl5Substitution(s);
1135: } else {
1136: substitution = new StringSubstitution(s);
1137: }
1138: } else {
1139: substitution = new AnyUtils.FunctionSubstitute(context,
1140: replacement);
1141: }
1142:
1143: Pattern pattern = ObjectPool.createPattern(context, pattern_);
1144: Perl5Matcher matcher = new Perl5Matcher();
1145: return Any.create(Util.substitute(matcher, pattern,
1146: substitution, _string, flags));
1147: }
1148:
1149: /// @method substitute
1150: /// Substitutes all regions from this string matching given
1151: /// regular expression.
1152: /// @synopsis string substitute(string pattern, string replacement [, int amount] )
1153: /// @synopsis string substitute(pattern pattern, string replacement [, int amount] )
1154: /// @synopsis string substitute(string pattern, Function replacer [, int amount] )
1155: /// @synopsis string substitute(pattern pattern, Function replacer [, int amount] )
1156: /// @param pattern Regular expression pattern
1157: /// @param replacement Replacement string
1158: /// @param replacer Replace function:
1159: /// @param amount Number of occurences to replace, if omitted, all occurences are substituted.
1160: /// <code>function replacer(list matchedGroups)</code>.
1161: /// @throws MalformedPattern If pattern is invalid
1162: public static final Object[] p_substitute = new Object[] { null,
1163: "pattern", "replacement", "*amount", null };
1164:
1165: public Any m_substitute(Context context, Any pattern,
1166: Any replacement, Any amount) {
1167: int flag = Util.SUBSTITUTE_ALL;
1168: if (amount != null) {
1169: flag = amount.toInt();
1170: }
1171: return doSubstitute(context, pattern, replacement, flag);
1172: }
1173:
1174: /// @method substituteFirst
1175: /// Substitutes first region from this string matching given
1176: /// regular expression.
1177: /// @synopsis string substituteFirst(string pattern, string replacement)
1178: /// @synopsis string substituteFirst(pattern pattern, string replacement)
1179: /// @synopsis string substituteFirst(string pattern, Function replacer)
1180: /// @synopsis string substituteFirst(pattern pattern, Function replacer)
1181: /// @param pattern Regular expression pattern
1182: /// @param replacement Replacement string
1183: /// @param replacer Replace function:
1184: /// <code>function replacer(list matchedGroups)</code>.
1185: /// @throws MalformedPattern If pattern is invalid
1186: public static final Object[] p_substituteFirst = new Object[] {
1187: null, "pattern", "replacement" };
1188:
1189: public Any m_substituteFirst(Context context, Any pattern,
1190: Any replacement) {
1191: return doSubstitute(context, pattern, replacement, 1);
1192: }
1193:
1194: /// @method hexToBinary
1195: /// Converts string to binary,
1196: /// assuming that two characters produce one byte: 414243 -> binary(abc)
1197: /// @synopsis binary hexToBinary()
1198: public Any m_hexToBinary() {
1199: String string = _string;
1200: int n = string.length() / 2;
1201: byte[] array = new byte[n];
1202: for (int i = 0; i < n; i++) {
1203: array[i] = (byte) ((Conversions.parseHex(string.charAt(i
1204: + i)) << 4) + Conversions.parseHex(string.charAt(i
1205: + i + 1)));
1206: }
1207: return new AnyBinary(array);
1208: }
1209:
1210: /// @method pad
1211: /// Pads text left or right to field.
1212: /// @synopsis string pad(int length)
1213: /// @synopsis string pad(int length, string padding)
1214: /// @param length Length of field, positive for right padding, negative
1215: /// for left padding.
1216: /// @param padding Padding character, default is space.
1217: /// @return Given text padded to field
1218: public static final Object[] p_pad = new Object[] { "length",
1219: "*pad", null };
1220:
1221: public Any m_pad(int length, String pad) {
1222: if (pad == null) {
1223: pad = " ";
1224: }
1225: boolean reverse = false;
1226: if (length < 0) {
1227: reverse = true;
1228: length = -length;
1229: }
1230: StringBuffer buffer = new StringBuffer(length);
1231: int i = _string.length();
1232: int padlen = pad.length();
1233: if (reverse) {
1234: buffer.append(_string);
1235: }
1236: while (i < length) {
1237: if (i + padlen > length) {
1238: buffer.append(pad.substring(0, length - i));
1239: break;
1240: } else {
1241: buffer.append(pad);
1242: i += padlen;
1243: }
1244: }
1245: if (!reverse) {
1246: buffer.append(_string);
1247: }
1248: return new AnyString(buffer.toString());
1249: }
1250:
1251: /// @method chop
1252: /// Chops suffix out of string.
1253: /// @synopsis string chop() ; Chops all whitespaces
1254: /// @synopsis string chop(string suffix) ; Chop given suffix
1255: public static final Object[] p_chop = new Object[] { "*suffix",
1256: null };
1257:
1258: public Any m_chop(String suffix) {
1259: String str = _string;
1260: int len = str.length();
1261: if (suffix != null) {
1262: if (str.endsWith(suffix)) {
1263: return Any.create(str.substring(0, len
1264: - suffix.length()));
1265: }
1266: } else {
1267: int i = len;
1268: while ((i > 0) && Character.isWhitespace(str.charAt(i - 1))) {
1269: i--;
1270: }
1271: if (i < len) {
1272: return Any.create(str.substring(0, i));
1273: }
1274: }
1275: return this ;
1276: }
1277:
1278: /// @method eat
1279: /// Removes given prefix out of string.
1280: /// @synopsis string eat() ; Removes all whitespaces
1281: /// @synopsis string eat(string prefix) ; Removes given prefix
1282: public static final Object[] p_eat = new Object[] { "*prefix", null };
1283:
1284: public Any m_eat(String prefix) {
1285: String str = _string;
1286: int len = str.length();
1287: if (prefix != null) {
1288: if (str.startsWith(prefix)) {
1289: return Any.create(str.substring(prefix.length()));
1290: }
1291: } else {
1292: int i = 0;
1293: while ((i < len) && Character.isWhitespace(str.charAt(i))) {
1294: i++;
1295: }
1296: if (i > 0) {
1297: return Any.create(str.substring(i));
1298: }
1299: }
1300: return this ;
1301: }
1302:
1303: transient public static final anvil.script.compiler.NativeClass __class__ = new anvil.script.compiler.NativeClass(
1304: "string", AnyString.class,
1305: AnySequence.__class__,
1306: //DOC{{
1307: ""
1308: + "\n"
1309: + " @class string\n"
1310: + " The <code>string</code> class represents character strings. \n"
1311: + " String is a sequence of characters.\n"
1312: + " @constructor string\n"
1313: + " Creates new string from the string representation of given parameter.\n"
1314: + " @synopsis string(object value)\n"
1315: + " @method length\n"
1316: + " Returns the length of this string.\n"
1317: + " @synopsis int length()\n"
1318: + " @method unquote\n"
1319: + " Unquotes all html entities from this string.\n"
1320: + " @synopsis string unquote()\n"
1321: + " @method quote\n"
1322: + " Converts all applicable characters with corresponding\n"
1323: + " html entity.\n"
1324: + " @synopsis string quote()\n"
1325: + " @method quoteMeta\n"
1326: + " Converts all applicable characters under >127 with corresponding\n"
1327: + " html entity.\n"
1328: + " @synopsis string quoteMeta()\n"
1329: + " @method quoteText\n"
1330: + " Converts all applicable characters over >127 with corresponding\n"
1331: + " html entity.\n"
1332: + " @synopsis string quoteText()\n"
1333: + " @method compress\n"
1334: + " Compresses consequtive white space characeters into single\n"
1335: + " space.\n"
1336: + " @synopsis string compress()\n"
1337: + " @method capitalize\n"
1338: + " Capitalizes the first character of all words.\n"
1339: + " @synopsis string capitalize()\n"
1340: + " @method capitalizeFirst\n"
1341: + " Capitalizes first character of first word.\n"
1342: + " @synopsis string capitalizeFirst()\n"
1343: + " @method nowrap\n"
1344: + " Converts white spaces into <code>&nbsp;</code>.\n"
1345: + " @synopsis string nowrap()\n"
1346: + " @method encodeURL\n"
1347: + " URL encodes this string.\n"
1348: + " @synopsis string encodeURL()\n"
1349: + " @method decodeURL\n"
1350: + " Decodes URL encoding from this string.\n"
1351: + " @synopsis string decodeURL()\n"
1352: + " @method encode\n"
1353: + " URL encodes this string.\n"
1354: + " @synopsis string encode()\n"
1355: + " @method decode\n"
1356: + " Decodes URL encoding from this string.\n"
1357: + " @synopsis string decode()\n"
1358: + " @method escape\n"
1359: + " Escapes string using the rules of anvil string literal.\n"
1360: + " @synopsis string escape()\n"
1361: + " @method unescape\n"
1362: + " Unescapes backslash quotes.\n"
1363: + " @synopsis string unescape()\n"
1364: + " @synopsis string unescape(boolean withQuotes)\n"
1365: + " @method nl2br\n"
1366: + " Convers all line feeds into <code><br>\n</code>\n"
1367: + " @synopsis string nl2br()\n"
1368: + " @method toUpper\n"
1369: + " Converts all lower case characeters into corresponding upper case\n"
1370: + " characeters.\n"
1371: + " @synopsis string toUpper()\n"
1372: + " @method toLower\n"
1373: + " Converts all upper case characeters into corresponding lower case\n"
1374: + " characeters.\n"
1375: + " @synopsis string toLower()\n"
1376: + " @method trim\n"
1377: + " Removes whitespace characters from the beginning and end of this string.\n"
1378: + " @synopsis string trim()\n"
1379: + " @method trimLeft\n"
1380: + " Removes whitespace characters from the beginning this string.\n"
1381: + " @synopsis string trimLeft()\n"
1382: + " @method trimRight\n"
1383: + " Removes whitespace characters from the end this string.\n"
1384: + " @synopsis string trimRight()\n"
1385: + " @method substring\n"
1386: + " Returns a substring from the given region of this string.\n"
1387: + " @synopsis string substring(int start)\n"
1388: + " @synopsis string substring(int start, int end)\n"
1389: + " @param start Starting index, included\n"
1390: + " @param end Ending index, excluded\n"
1391: + " @method charAt\n"
1392: + " Returns single character from this string.\n"
1393: + " @synopsis string charAt(int index)\n"
1394: + " @param index Index to string\n"
1395: + " @method charCodeAt\n"
1396: + " Returns the character code at the speicified index.\n"
1397: + " @synopsis int charCodeAt(int index)\n"
1398: + " @param index Index to string\n"
1399: + " @method indexOf\n"
1400: + " Returns the index within this string of the first occurrence of the \n"
1401: + " specified string.\n"
1402: + " @synopsis int indexOf(string needle)\n"
1403: + " @synopsis int indexOf(string needle, int fromIndex)\n"
1404: + " @param needle String to search for\n"
1405: + " @param fromIndex Starting index of search\n"
1406: + " @return The index of first occurence, or -1 if it doesn't occur.\n"
1407: + " @method lastIndexOf\n"
1408: + " Returns the index within this string of the last occurrence of the \n"
1409: + " specified string.\n"
1410: + " @synopsis int lastIndexOf(string needle)\n"
1411: + " @synopsis int lastIndexOf(string needle, int fromIndex)\n"
1412: + " @param needle String to search for\n"
1413: + " @param fromIndex Starting index of search\n"
1414: + " @return The index of last occurence, or -1 if it doesn't occur.\n"
1415: + " @method startsWith\n"
1416: + " Checks if this string starts with given prefix.\n"
1417: + " @synopsis boolean startsWith(string prefix)\n"
1418: + " @method endsWith\n"
1419: + " Checks if this string ends with given suffix.\n"
1420: + " @synopsis boolean endsWith(string suffix)\n"
1421: + " @method compareToIgnoreCase\n"
1422: + " Compares this string and given string, lower- and\n"
1423: + " uppercase characters are considered equal.\n"
1424: + " @synopsis int compareToIgnoreCase(string other)\n"
1425: + " @return -1 if <code>this<other</code>,\n"
1426: + " 0 if <code>this==other</code>,\n"
1427: + " 1 if <code>this>other</code>,\n"
1428: + " @method equalsIgnoreCase\n"
1429: + " Checks if this string is equal to given string, lower- and\n"
1430: + " uppercase characters are considered equal\n"
1431: + " @synopsis boolean equalsIgnoreCase(string other)\n"
1432: + " @param other String to compare with\n"
1433: + " @method explode\n"
1434: + " Splits this string into regions using given set of delimiter \n"
1435: + " characters.\n"
1436: + " @synopsis list explode(string delimiters)\n"
1437: + " @synopsis list explode(string delimiters, boolean returnEmpty)\n"
1438: + " @param delimiters Set of delimiter characters\n"
1439: + " @param returnEmpty If <code>true</code> also empty regions are returned, if\n"
1440: + " omitted or <code>false</code> empty regions are not returned.\n"
1441: + " @method repeat\n"
1442: + " Repeats this string <code>amount</code> times.\n"
1443: + " @synopsis string repeat(int amount)\n"
1444: + " @method translate\n"
1445: + " For each character in this string: if character appears\n"
1446: + " in <code>from</code> string, converts it to character \n"
1447: + " at the same index at <code>to</code>.\n"
1448: + " @synopsis string translate(string from, string to)\n"
1449: + " @param from Source character mapping\n"
1450: + " @param to Target character mapping\n"
1451: + " @return Translated string\n"
1452: + " @method replace\n"
1453: + " Replaces all occurences of <code>from</code> into <code>to</code>.\n"
1454: + " @synopsis string replace(string from, string to)\n"
1455: + " @param from Regions to replace\n"
1456: + " @param to Replacement\n"
1457: + " @method unserialize\n"
1458: + " Unserializes data contained in this string.\n"
1459: + " @synopsis object unserialize()\n"
1460: + " @return Unserialized data\n"
1461: + " @throws CorruptedSerialization If serialized data is corrupted\n"
1462: + " @method matches\n"
1463: + " Checks if this (whole) string matches given \n"
1464: + " regular expression. \n"
1465: + " @synopsis boolean matches(string pattern)\n"
1466: + " @synopsis boolean matches(string pattern, array matches)\n"
1467: + " @synopsis boolean matches(string pattern, list matches)\n"
1468: + " @synopsis boolean matches(pattern pattern)\n"
1469: + " @synopsis boolean matches(pattern pattern, array matches)\n"
1470: + " @synopsis boolean matches(pattern pattern, list matches)\n"
1471: + " @param pattern Regular expression pattern\n"
1472: + " @return Matches or boolean\n"
1473: + " @throws MalformedPattern If pattern is invalid\n"
1474: + " @method contains\n"
1475: + " Checks if some region of this string matches given \n"
1476: + " regular expression.\n"
1477: + " @synopsis boolean contains(string pattern)\n"
1478: + " @synopsis boolean contains(string pattern, array matches)\n"
1479: + " @synopsis boolean contains(string pattern, list matches)\n"
1480: + " @synopsis boolean contains(pattern pattern)\n"
1481: + " @synopsis boolean contains(pattern pattern, array matches)\n"
1482: + " @synopsis boolean contains(pattern pattern, list matches)\n"
1483: + " @param pattern Regular expression pattern\n"
1484: + " @return Matches or boolean\n"
1485: + " @throws MalformedPattern If pattern is invalid\n"
1486: + " @method find\n"
1487: + " Finds all regions from this string mathing given regular\n"
1488: + " expression pattern.\n"
1489: + " @synopsis enumeration find(string pattern)\n"
1490: + " @synopsis enumeration find(pattern pattern)\n"
1491: + " @param pattern Regular expression pattern\n"
1492: + " @return Enumeration of strings\n"
1493: + " @throws MalformedPattern If pattern is invalid\n"
1494: + " @method split\n"
1495: + " Splits this string using given regular expression pattern as \n"
1496: + " delimiter.\n"
1497: + " @synopsis list split(string pattern)\n"
1498: + " @synopsis list split(pattern pattern)\n"
1499: + " @param pattern Regular expression pattern\n"
1500: + " @throws MalformedPattern If pattern is invalid\n"
1501: + " @method divide\n"
1502: + " Divides string to two pieces, using <code>delim</code> as delimiter.\n"
1503: + " Example 1. \"foo.bar.foo\".divide(\".\") returns (\"foo\", \"bar.foo\")\n"
1504: + " Example 2. \"foo.bar.foo\".divide(\"|\") returns (\"foo.bar.foo\", null)\n"
1505: + " Example 3. \"foo.bar.fiu\".divide(\".\", true) return (\"fiu\", \"foo.bar\")\n"
1506: + " @synopsis list divide(string delim)\n"
1507: + " @synopsis list divide(string delim, boolean dir)\n"
1508: + " @param delim Delimiter character(s)\n"
1509: + " @param dir Direction (default false), false = from start of the string,\n"
1510: + " true = from end of the string\n"
1511: + " @method substitute\n"
1512: + " Substitutes all regions from this string matching given\n"
1513: + " regular expression.\n"
1514: + " @synopsis string substitute(string pattern, string replacement [, int amount] )\n"
1515: + " @synopsis string substitute(pattern pattern, string replacement [, int amount] )\n"
1516: + " @synopsis string substitute(string pattern, Function replacer [, int amount] )\n"
1517: + " @synopsis string substitute(pattern pattern, Function replacer [, int amount] )\n"
1518: + " @param pattern Regular expression pattern\n"
1519: + " @param replacement Replacement string\n"
1520: + " @param replacer Replace function: \n"
1521: + " @param amount Number of occurences to replace, if omitted, all occurences are substituted.\n"
1522: + " <code>function replacer(list matchedGroups)</code>.\n"
1523: + " @throws MalformedPattern If pattern is invalid\n"
1524: + " @method substituteFirst\n"
1525: + " Substitutes first region from this string matching given\n"
1526: + " regular expression.\n"
1527: + " @synopsis string substituteFirst(string pattern, string replacement)\n"
1528: + " @synopsis string substituteFirst(pattern pattern, string replacement)\n"
1529: + " @synopsis string substituteFirst(string pattern, Function replacer)\n"
1530: + " @synopsis string substituteFirst(pattern pattern, Function replacer)\n"
1531: + " @param pattern Regular expression pattern\n"
1532: + " @param replacement Replacement string\n"
1533: + " @param replacer Replace function: \n"
1534: + " <code>function replacer(list matchedGroups)</code>.\n"
1535: + " @throws MalformedPattern If pattern is invalid\n"
1536: + " @method hexToBinary \n"
1537: + " Converts string to binary, \n"
1538: + " assuming that two characters produce one byte: 414243 -> binary(abc) \n"
1539: + " @synopsis binary hexToBinary()\n"
1540: + " @method pad\n"
1541: + " Pads text left or right to field.\n"
1542: + " @synopsis string pad(int length)\n"
1543: + " @synopsis string pad(int length, string padding)\n"
1544: + " @param length Length of field, positive for right padding, negative\n"
1545: + " for left padding.\n"
1546: + " @param padding Padding character, default is space.\n"
1547: + " @return Given text padded to field\n"
1548: + " @method chop\n"
1549: + " Chops suffix out of string.\n"
1550: + " @synopsis string chop() ; Chops all whitespaces\n"
1551: + " @synopsis string chop(string suffix) ; Chop given suffix\n"
1552: + " @method eat\n"
1553: + " Removes given prefix out of string.\n"
1554: + " @synopsis string eat() ; Removes all whitespaces\n"
1555: + " @synopsis string eat(string prefix) ; Removes given prefix\n"
1556: //}}DOC
1557: );
1558:
1559: }
|