## \$PIECE

### 7.1.5.12 \$Piece

\$P[IECE] ( expr1 , expr2 )

This form is defined here with the aid of a function, NF, which is used for definitional purposes only, called find the position number following the mth occurrence.

NF(s,d,m) is defined, for strings s, d, and integer m, as follows:

• When d is the empty string, the result is zero.
• When m '> 0, the result is zero.
• When d is not a substring of s, i.e., when \$F(s,d) = 0, then the result is \$L(s) + \$L(d) + 1.
• Otherwise, NF(s,d,1) = \$F(s,d).
• For m > 1, NF(s,d,m) = NF(\$E(s,\$F(s,d),\$L(s)),d,m–1) + \$F(s,d) - 1.
• That is, NF extends \$Find to give the position number of the character to the right of the mth occurrence of the string d in s.

Let s be the value of expr1, and let d be the value of expr2. \$Piece(s,d) returns the substring of s bounded on the right but not including the first (leftmost) occurrence of d.

\$P(s,d) = \$E(s,0,NF(s,d,1) - \$L(d) - 1).

\$P[IECE] ( expr1 , expr2 , intexpr )

Let m be the integer value of intexpr. \$Piece(s,d,m) returns the substring of s bounded by but not including the m–1th and the mth occurrence of d.

\$P(s,d,m) = \$E(s,NF(s,d,m–1),NF(s,d,m) - \$L(d) - 1).

\$P[IECE] ( expr1 , expr2 , intexpr1 , intexpr2 )

Let n be the integer value of intexpr2. \$Piece(s,d,m,n) returns the substring of s bounded on the left but not including the m–1th occurrence of d in s, and bounded on the right but not including the nth occurrence of d in s.

\$P(s,d,m,n) = \$E(s,NF(s,d,m–1),NF(s,d,n) - \$L(d) - 1).

Note that \$P(s,d,m,m) = \$P(s,d,m), and that \$P(s,d,1) = \$P(s,d).