export interface CodeBlock { start : number ; end : number ; code : string ; } param srcContents param insertion param offset returns export function insertContentsAtOffset ( srcContents , insertion , offset ) { const srcContentsLength = srcContents . length ; if ( offset < 0 || offset > srcContentsLength ) { throw new Error ( 'Invalid parameters.' ) ; } if ( offset === 0 ) { return `${ insertion }${ srcContents }` ; } else if ( offset === srcContentsLength ) { return `${ srcContents }${ insertion }` ; } const prefix = srcContents . substring ( 0 , offset ) ; const suffix = srcContents . substring ( offset ) ; return `${ prefix }${ insertion }${ suffix }` ; } param contents param replacement param startOffset param endOffset returns export function replaceContentsWithOffset ( contents , replacement , startOffset , endOffset ) { const contentsLength = contents . length ; if ( startOffset < 0 || endOffset < 0 || startOffset >= contentsLength || endOffset >= contentsLength || startOffset > endOffset ) { throw new Error ( 'Invalid parameters.' ) ; } const prefix = contents . substring ( 0 , startOffset ) ; const suffix = contents . substring ( endOffset + 1 ) ; return `${ prefix }${ replacement }${ suffix }` ; } param source param regexp param offset returns export function searchFromOffset ( source , regexp , offset ) { const target = source . substring ( offset ) ; const matchedIndex = target . search ( regexp ) ; return matchedIndex < 0 ? matchedIndex : matchedIndex + offset ; }