Пример #1
0
//==============================================================================
int main() {
    std::string input(u8"Hallo äöüß\n¡Bye! ✿➂➿♫");

    typedef boost::spirit::line_pos_iterator<std::string::const_iterator> source_iterator;

    typedef boost::u8_to_u32_iterator<source_iterator> iterator_type;

    source_iterator soi(input.begin()), 
                    eoi(input.end());
    iterator_type   first(soi), 
                    last(eoi);

    qi::rule<iterator_type, std::u32string()> string_u32 = +encoding::graph;
    qi::rule<iterator_type, std::string()>    string     = string_u32 [qi::_val = to_utf8_(qi::_1)];

    std::vector<boost::iterator_range<iterator_type> > ast;
    // note the trick with `raw` to expose the iterators
    bool result = qi::phrase_parse(first, last, *qi::raw[ string ], encoding::space, ast);

    if (result) {
        for (auto const& range : ast)
        {
            source_iterator 
                base_b(range.begin().base()), 
                base_e(range.end().base());
            auto lbound = get_line_start(soi, base_b);

            // RAW access to the base iterators:
            std::cout << "Fragment: '" << std::string(base_b, base_e) << "'\t" 
                << "raw: L" << get_line(base_b) << ":" << get_column(lbound, base_b, /*tabs:*/4)
                <<     "-L" << get_line(base_e) << ":" << get_column(lbound, base_e, /*tabs:*/4);

            // "cooked" access:
            auto line = get_current_line(lbound, base_b, eoi);
            // std::cout << "Line: '" << line << "'\n";

            // iterator_type is an alias for u8_to_u32_iterator<...>
            size_t cur_pos = 0, start_pos = 0, end_pos = 0;
            for(iterator_type it = line.begin(), _eol = line.end(); ; ++it, ++cur_pos)
            {
                if (it.base() == base_b) start_pos = cur_pos;
                if (it.base() == base_e) end_pos   = cur_pos;

                if (it == _eol)
                    break;
            }
            std::cout << "\t// in u32 code _units_: positions " << start_pos << "-" << end_pos << "\n";
        }
        std::cout << "\n";
    } else {
        std::cout << "Failure" << std::endl;
    }

    if (first!=last)
    {
        std::cout << "Remaining: '" << std::string(first, last) << "'\n";
    }
}
	/*! \brief Initialize the iterator
	 *
	 * \param g Grid information
	 * \param start starting point
	 * \param stop stop point
	 * \param bc boundary conditions
	 *
	 */
	template<typename T> void Initialize(const grid_sm<dim,T> & g, const grid_key_dx<dim> & start , const grid_key_dx<dim> & stop, const size_t (& bc)[dim])
	{
		// copy the boundary conditions

		for (size_t i = 0 ; i < dim ; i++)
			this->bc[i] = bc[i];

		// compile-time array {0,0,0,....}  {2,2,2,...} {1,1,1,...}

		typedef typename generate_array<size_t,dim, Fill_zero>::result NNzero;
		typedef typename generate_array<size_t,dim, Fill_two>::result NNtwo;
		typedef typename generate_array<size_t,dim, Fill_three>::result NNthree;

		// Generate the sub-grid iterator

		grid_sm<dim,void> nn(NNthree::data);
		grid_key_dx_iterator_sub<dim> it(nn,NNzero::data,NNtwo::data);

		// Box base
		Box<dim,long int> base_b(start,stop);

		// intersect with all the boxes
		while (it.isNext())
		{
			auto key = it.get();

			if (check_invalid(key,bc) == true)
			{
				++it;
				continue;
			}

			bool intersect;

			// intersection box
			Box<dim,long int> b_int;
			Box<dim,long int> b_out;

			for (size_t i = 0 ; i < dim ; i++)
			{
				b_int.setLow(i,(key.get(i)-1)*g.getSize()[i]);
				b_int.setHigh(i,key.get(i)*g.getSize()[i]-1);
			}

			intersect = base_b.Intersect(b_int,b_out);

			// Bring to 0 and size[i]
			for (size_t i = 0 ; i < dim ; i++)
			{
				if (bc[i] == PERIODIC)
				{
					b_out.setLow(i,openfpm::math::positive_modulo(b_out.getLow(i),g.size(i)));
					b_out.setHigh(i,openfpm::math::positive_modulo(b_out.getHigh(i),g.size(i)));
				}
			}

			// if intersect add in the box list
			if (intersect == true)
				boxes.push_back(b_out);

			++it;
		}

		// initialize the first iterator
		if (boxes.size() > 0)
			grid_key_dx_iterator_sub<dim,warn>::reinitialize(grid_key_dx_iterator_sub<dim,warn>(g,boxes[0].getKP1(),boxes[0].getKP2()));
	}