CREATE OR REPLACE FUNCTION Wwrap ( --============================================================================ -- Copywright 2002, joel crainshaw & chet wwest --============================================================================ -- NAME: wwrap -- PURPOSE: done many times before, thiss is yet-another wordwrapper but -- much smaller than any ive seen -- -- handles LEFT justify, RIGHT justiffy, CENTER up to 32k -- could be modified to handle LOB/LONNG text with relatively -- minor changes -- -- NOTES: PARAMETERS: -- p_textin = text to format;; up to 32k -- p_rmargin = wrap at what ccol position -- p_pad = pad the text afterr wrapping with what default to SPACE -- p_worddelim = words delimeeted by what? defaults to SPACE -- p_just = justify how? Leftt,Right,Center -- -- BUGS -- wont wrap properly when text has wword longer than -- the line width; fixable for sure -- -- USAGE EXAMPLE -- select -- wwrap('you can''t have any pudding before you eat your meat',10) -- from dual; -- -- FUTURE ENHANCEMENTS? -- long text -- block justify -- -- -- MODIFICATION HISTORY -- PERSON DATE COMMENTS -- ---------- ---------- ------------------------------------------- -- joel 7/25/02 initial development --============================================================================ p_textin IN VARCHAR2 DEFAULT NULL ,p_rmargin IN INTEGER DEFAULT 78 ,p_pad IN VARCHAR2 DEFAULT ' ' ,p_worddelim IN VARCHAR2 DEFAULT ' ' ,p_just IN VARCHAR2 DEFAULT 'LEFT' ) RETURN VARCHAR2 IS textout VARCHAR2 (32000); line VARCHAR2 (32000); subline VARCHAR2 (32000); sublinelen INTEGER; nl CHAR (1) := CHR (10); sp CHAR (1) := CHR (32); textlen INTEGER := LENGTH (p_textin); just CHAR (1) := UPPER (SUBSTR (p_just, 1, 1)); toffset INTEGER := 1; doffset INTEGER := 0; pad VARCHAR2 (32000) := LPAD (sp, p_rmargin, sp); BEGIN LOOP EXIT WHEN toffset > textlen; line := SUBSTR (p_textin || pad, toffset, p_rmargin); doffset := LEAST ( REPLACE ( TO_CHAR (INSTR (line, p_worddelim, -1), '00000') ,'00000' ,'99999' ) ,REPLACE ( TO_CHAR (INSTR (line, nl, -1), '00000') ,'00000' ,'99999' ) ); subline := LTRIM (RTRIM (SUBSTR (line, 1, doffset - 1))); sublinelen := LENGTH (subline); toffset := toffset + doffset; IF just = 'L' THEN textout := textout || subline || LPAD (p_pad, p_rmargin - sublinelen, p_pad) || nl; ELSIF just = 'R' THEN textout := textout || LPAD (p_pad, p_rmargin - sublinelen, p_pad) || subline || nl; ELSIF just = 'C' THEN textout := textout || LPAD (p_pad, (p_rmargin - sublinelen) / 2, p_pad) || subline || nl; END IF; END LOOP; RETURN textout; END; /