чрезвычайно дружелюбный replacer

    Date: 03/22/07 (Code WTF)    Keywords: xml, java, google

    Есть такая контора - ThoughtWorks. И есть у неё такая библиотечка - XStream. Довольно широко используемая.

    И есть в ней такой замечательный кусок кода. Можете посчитать в нём количество ненужных аллокаций объектов.


    /**
         * Unescapes name re-enstating '$' and '_' when replacement strings are found
         * @param name the name of attribute or node
         * @return The String with unescaped name
         */
        public String unescapeName(String name) {
            StringBuffer result = new StringBuffer();
            int length = name.length();
            for(int i = 0; i < length; i++) {
                char c = name.charAt(i);
                if ( stringFoundAt(name, i, underscoreReplacement)) {
                    i += underscoreReplacement.length() - 1;
                    result.append('_');
                } else if ( stringFoundAt(name, i, dollarReplacement)) {
                    i += dollarReplacement.length() - 1;
                    result.append('$');
                } else {
                    result.append(c);
                }
            }
            return result.toString();
        }
    
        private boolean stringFoundAt(String name, int i, String replacement) {
            if ( name.length() >= i + replacement.length()
              && name.substring(i, i + replacement.length()).equals(replacement) ){
                return true;
            }
            return false;
        }


    http://www.google.com/codesearch?hl=en&q=+package:%22http://dist.codehaus.org/xstream/distributions/xstream-1.2.zip%22+xmlfriendlyreplacer+show:ITug5ohLr3E:OnvyQYwElLE:aFFngwu4ecM&sa=N&cd=1&ct=rc&cs_p=http://dist.codehaus.org/xstream/distributions/xstream-1.2.zip&cs_f=xstream-1.2/src/java/com/thoughtworks/xstream/io/xml/XmlFriendlyReplacer.java#a0

    Source: http://community.livejournal.com/code_wtf/74748.html

« Американские... || Не прикол с VS 2005 »


antivirus | apache | asp | blogging | browser | bugtracking | cms | crm | css | database | ebay | ecommerce | google | hosting | html | java | jsp | linux | microsoft | mysql | offshore | offshoring | oscommerce | php | postgresql | programming | rss | security | seo | shopping | software | spam | spyware | sql | technology | templates | tracker | virus | web | xml | yahoo | home