Beispiel #1
0
 types::ndarray<typename types::numpy_type<dtype>::type, 1>
 fromstring(types::str const &string, dtype d, long count,
            types::str const &sep)
 {
   if (sep) {
     types::list<typename types::numpy_type<dtype>::type> res(0);
     if (count < 0)
       count = std::numeric_limits<long>::max();
     else
       res.reserve(count);
     size_t current;
     size_t next = -1;
     long numsplit = 0;
     do {
       current = next + 1;
       next = string.find_first_of(sep, current);
       typename types::numpy_type<dtype>::type item;
       std::istringstream iss(
           string.substr(current, next - current).get_data());
       iss >> item;
       res.push_back(item);
     } while (next != types::str::npos && ++numsplit < count);
     return {res};
   } else {
     if (count < 0)
Beispiel #2
0
 bool endswith(types::str const& s, types::str const& suffix, long start, long end)
 {
     if(end == -1)
         end = s.size();
     long rstart = end - suffix.size();
     return rstart >= start and s.compare(rstart, suffix.size(), suffix) == 0;
 }
Beispiel #3
0
 void file::write(types::str const &str)
 {
   if (not is_open)
     throw ValueError("I/O operation on closed file");
   if (mode.find_first_of("wa+") == -1)
     throw IOError("file.write() :  File not opened for writing.");
   fwrite(str.c_str(), sizeof(char), str.size(), **data);
 }
Beispiel #4
0
long find(types::str const &s, types::str const &value, long start,
          long end)
{
    if (end < 0)
        end += s.size();
    long a = s.find(value, start);
    return (a > end) ? -1 : a;
}
Beispiel #5
0
 bool startswith(types::str const &s, types::str const &prefix, long start,
                 long end)
 {
   if (end < 0)
     end = s.size();
   return (end - start) >= prefix.size() and
          s.compare(start, prefix.size(), prefix) == 0;
 }
Beispiel #6
0
 types::str capitalize(types::str const &s)
 {
   if (s.empty())
     return s;
   else {
     types::str copy = s;
     copy[0] = ::toupper(s[0]);
     std::transform(s.begin() + 1, s.end(), copy.begin() + 1, ::tolower);
     return copy;
   }
 }
Beispiel #7
0
void _join(types::str &buffer, T &&head, Types &&... tail)
{
    if (head[0] == '/')
        buffer = std::forward<T>(head);
    else if (not buffer or *buffer.rbegin() == OS_SEP or
             *buffer.rbegin() == '/')
        buffer += std::forward<T>(head);
    else {
        buffer += OS_SEP;
        buffer += std::forward<T>(head);
    }
    _join(buffer, std::forward<Types>(tail)...);
}
Beispiel #8
0
 long str::count(types::str const &sub) const
 {
   long counter = 0;
   for (long z = find(sub);            // begin by looking for sub
        z != -1;                       // as long as we don't reach the end
        z = find(sub, z + sub.size())) // look for another one
   {
     ++counter;
   }
   return counter;
 }
Beispiel #9
0
    // Modifiers
    void file::open(types::str const &filename, types::str const &strmode)
    {
      const char *smode = strmode.c_str();
      // Python enforces that the mode, after stripping 'U', begins with 'r',
      // 'w' or 'a'.
      if (*smode == 'U') {
        ++smode;
      } // Not implemented yet

      data = utils::shared_ref<container_type>(filename, smode);
      if (not**data)
        throw types::IOError("Couldn't open file " + filename);
      is_open = true;
    }
Beispiel #10
0
      types::str join(S const &s, types::str const &iterable)
      {
        long ssize = std::distance(std::begin(s), std::end(s)) -
                     (std::is_same<S, types::str>::value ? 0 : 1);
        /* first iterate over iterable to gather sizes */
        size_t n = ssize * (iterable.size() - 1) + iterable.size();

        std::string out(n, 0);

        auto iter = iterable.begin();
        auto oter = out.begin();
        if (iter != iterable.end()) {
          *oter++ = *iter++;
          if (ssize)
            for (; iter != iterable.end(); ++iter) {
              oter = std::copy(std::begin(s), std::begin(s) + ssize, oter);
              *oter++ = *iter;
            }
          else
            std::copy(iter, iterable.end(), oter);
        }
        return {std::move(out)};
      }
Beispiel #11
0
 types::str lstrip(types::str const& self, types::str const& to_del = " ")
 {
     return types::str(self.begin() + self.find_first_not_of(to_del), self.end());
 }
Beispiel #12
0
 _file::_file(types::str const &filename, types::str const &strmode)
     : f(fopen(filename.c_str(), strmode.c_str())), _buffer(nullptr),
       _buffer_size(0)
 {
 }
Beispiel #13
0
long find(types::str const &s, types::str const &value, long start)
{
    return find(s, value, start, s.size());
}
Beispiel #14
0
long find(types::str const &s, types::str const &value)
{
    return find(s, value, 0, s.size());
}
Beispiel #15
0
 bool startswith(types::str const& s, types::str const& prefix, long start=0, size_t end=std::string::npos) {
     if(end == std::string::npos)
         end = s.size();
     return (end - start) >= prefix.size() and s.compare(start, prefix.size(), prefix) == 0;
 }
Beispiel #16
0
 types::str upper(types::str const &s)
 {
   types::str copy = s;
   std::transform(s.begin(), s.end(), copy.begin(), ::toupper);
   return copy;
 }
Beispiel #17
0
 // TODO : no check on file existance?
 _file::_file(types::str const &filename, types::str const &strmode)
     : f(fopen(filename.c_str(), strmode.c_str()))
 {
 }
Beispiel #18
0
 types::str rstrip(types::str const &self, types::str const &to_del)
 {
   return {self.begin(), self.begin() + self.find_last_not_of(to_del) + 1};
 }
Beispiel #19
0
 bool isalpha(types::str const &s)
 {
   return !s.empty() && std::all_of(s.chars().begin(), s.chars().end(),
                                    (int (*)(int))std::isalpha);
 }