|
Description:
A recursive template to repeat a string a number of times in a manner similar to the Perl 'x' operator.
Source: Text Source
<xsl:template name="repeat-string">
<xsl:param name="str"/>
<xsl:param name="cnt"/>
<xsl:param name="pfx"/>
<xsl:choose>
<xsl:when test="$cnt = 0">
<xsl:value-of select="$pfx"/>
</xsl:when>
<xsl:when test="$cnt mod 2 = 1">
<xsl:call-template name="repeat-string">
<xsl:with-param name="str" select="concat($str,$str)"/>
<xsl:with-param name="cnt" select="($cnt - 1) div 2"/>
<xsl:with-param name="pfx" select="concat($pfx,$str)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="repeat-string">
<xsl:with-param name="str" select="concat($str,$str)"/>
<xsl:with-param name="cnt" select="$cnt div 2"/>
<xsl:with-param name="pfx" select="$pfx"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
The license for this recipe is available here.
Discussion:
The template is written recursively and uses the "binary method" to repeat the string in O(log n) time. The actual number of concat operations performed to repeat a string n times is log n + .
|