mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Implement switch statements with simple value comparison to get the drudge work of parsing and generating switches in place. Cases are checked using a '=~' operator to compare the subject of the switch to the value in the case. Unlike a C switch, cases each have their own scope and don't fall through. 'break' and 'continue' apply to an outer loop rather to the switch itself. Multiple case values can be specified in a comma-separated list, as in 'case 1, 2, 3, 4:'. Currently no effort is made to check for duplicate cases or to rank cases by match strength; cases are just checked in source order, and the first one wins (aside from 'default', which is branched to if all cases fail). Swift SVN r4359
47 lines
1.5 KiB
C++
47 lines
1.5 KiB
C++
//===- Interleave.h - for_each between sequence elements --------*- C++ -*-===//
|
|
//
|
|
// This source file is part of the Swift.org open source project
|
|
//
|
|
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
|
|
// Licensed under Apache License v2.0 with Runtime Library Exception
|
|
//
|
|
// See http://swift.org/LICENSE.txt for license information
|
|
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This file provides interleave, an STL-style algorithm similar to
|
|
// std::for_each that applies a second functor between every pair of elements.
|
|
// This provides the control flow logic to, for example, print a
|
|
// comma-separated list:
|
|
//
|
|
// interleave(names.begin(), names.end(),
|
|
// [&](StringRef name) { OS << name; },
|
|
// [&] { OS << ", "; });
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#ifndef SWIFT_BASIC_INTERLEAVE_H
|
|
#define SWIFT_BASIC_INTERLEAVE_H
|
|
|
|
namespace swift {
|
|
|
|
template <typename ForwardIterator,
|
|
typename UnaryFunctor,
|
|
typename NullaryFunctor>
|
|
inline void interleave(ForwardIterator begin, ForwardIterator end,
|
|
UnaryFunctor each_fn,
|
|
NullaryFunctor between_fn) {
|
|
if (begin == end)
|
|
return;
|
|
each_fn(*begin);
|
|
++begin;
|
|
for (; begin != end; ++begin) {
|
|
between_fn();
|
|
each_fn(*begin);
|
|
}
|
|
}
|
|
|
|
} // end namespace swift
|
|
|
|
#endif |