/**************************************************************************/
/*                                                                        */
/*                                 Sources                                */
/*                                from  the                               */
/*                           XSC SOFTWARE PACKAGE                         */
/*                                                                        */
/*                Copyright (c) 1997 University of Karlsruhe              */
/*                     Institute for Appl. Mathematics                    */
/*                                                                        */
/* This program is free software for non-commercial use. For details, see */
/* the file LICENSE.XSC, distributed with this software package.          */
/*                                                                        */
/* This program is distributed WITHOUT ANY WARRANTY. For details, see the */
/* file LICENSE.XSC, distributed with this software package.              */
/*                                                                        */
/**************************************************************************/

/* RCS $Id: s_suba.c,v 1.2 1997/09/25 18:03:59 ae59 Exp $ */
/*
$Log: s_suba.c,v $
 * Revision 1.2  1997/09/25  18:03:59  ae59
 * Copyright-message
 *
 * Revision 1.1  1994/01/27  13:24:52  ae59
 * Put under RCS control
 *
 */
/****************************************************************/
/*                                                              */
/*      Filename        : s_suba.c                              */
/*                                                              */
/*      Entries         : s_trng s_suba(s,pos,end)              */
/*                        s_trng s;                             */
/*                        a_intg pos,end;                       */
/*                                                              */
/*      Arguments       : s = string                            */
/*                        pos = position                        */
/*                        end = end position                    */
/*                                                              */
/*      Return value    : substring                             */
/*                                                              */
/*      Description     : Substring of string beginning at      */
/*                        position pos up to an including       */
/*                        position end                          */
/*                                                              */
/*      Date            : 1992-07-23                            */
/*      1992-07-23 : cast at memcpy corrected               =b= */
/*      1992-07-22 : cast to size_t, void* at n assignment, =b= */
/*                   memcpy call                                */
/*      1990-12-03 : new routine                                */
/****************************************************************/

#ifndef ALL_IN_ONE
#ifdef AIX
#include "/u/p88c/runtime/o_defs.h"
#else
#include "o_defs.h"
#endif
#define local
#endif

#ifdef LINT_ARGS
local s_trng s_suba(s_trng s,a_intg pos,a_intg end)
#else
local s_trng s_suba(s,pos,end)

s_trng s;
a_intg pos;
a_intg end;
#endif
        {
        s_trng res;
        size_t n;

        if (pos<=s.clen && end>=pos)
           {
           if (pos<1) pos = 1;
           n = (size_t) (((end<=s.clen) ? end : s.clen)-pos+1);
           s_init(&res,n);
           if (res.ptr!=NULL) memcpy((void*) res.ptr, (void*) (s.ptr+pos-1),n);
           res.clen = n;
           }
        else
           s_init(&res,(size_t)0);

        res.tmp = TRUE;

        if (s.tmp) s_free(&s);

#ifdef HEAP_CHECK
b_tmph((a_char *)&res.ptr);
#endif

        return(res);
        }

