プログラマの血液型

このエントリーをはてなブックマークに追加
360仕様書無しさん
>>356 ほい、置換関数(C++) B型作

template< class OIRES, class IISRC, class IIFIND, class IIREPLACE, class COPY_OP, class SEARCH_OP >
OIRES replace( OIRES out_begin, IISRC src_begin, IISRC src_end, IIFIND find_begin, IIFIND find_end,
        IIREPLACE replace_begin, IIREPLACE replace_end, COPY_OP copy_op, SEARCH_OP search_op,
        int count = -1 )
{
 if ( src_end <= src_begin ) return out_begin;
 if ( find_end <= find_begin ) return copy_op( src_begin, src_end, out_begin );
 if ( count == 0 ) return copy_op( src_begin, src_end, out_begin );
 if ( replace_end < replace_begin ) replace_end = replace_begin;
 size_t find_size = find_end - find_begin;
 size_t replace_size = replace_end - replace_begin;
 OIRES out_pos = out_begin;
 IISRC findpos;
 IISRC last_find = src_begin;
 int repcnt = 0;
 while ( findpos = search_op( last_find, src_end, find_begin, find_end ),
  findpos != src_end && ( count < 0 || repcnt < count ) ){
  out_pos = copy_op( last_find, findpos, out_pos );
  out_pos = copy_op( replace_begin, replace_end, out_pos );
  findpos += find_size;
  last_find = findpos;
  repcnt++;
 }
 out_pos = copy_op( last_find, src_end, out_pos );
 return out_pos;
}

//コメントがないのがB型らしい?