LibreOffice Module sw (master)  1
SwRewriter.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include <algorithm>
21 #include <SwRewriter.hxx>
22 
23 using namespace std;
24 
26 {
27 }
28 
29 void SwRewriter::AddRule(SwUndoArg eWhat, const OUString & rWith)
30 {
31  SwRewriteRule aRule(eWhat, rWith);
32 
33  vector<SwRewriteRule>::iterator aIt = find_if(
34  mRules.begin(), mRules.end(),
35  [&aRule](SwRewriteRule const & a) { return a.first == aRule.first; });
36 
37  if (aIt != mRules.end())
38  *aIt = aRule;
39  else
40  mRules.push_back(aRule);
41 }
42 
43 OUString SwRewriter::Apply(const OUString & rStr) const
44 {
45  OUString aResult = rStr;
46 
47  for (const auto& rRule : mRules)
48  {
49  aResult = aResult.replaceAll(GetPlaceHolder(rRule.first), rRule.second);
50  }
51 
52  return aResult;
53 }
54 
56 {
57  switch (eId)
58  {
59  case UndoArg1:
60  return OUString("$1");
61  case UndoArg2:
62  return OUString("$2");
63  case UndoArg3:
64  return OUString("$3");
65  default:
66  break;
67  }
68  return OUString("$1");
69 }
70 
71 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static OUString GetPlaceHolder(SwUndoArg eId)
Definition: SwRewriter.cxx:55
OUString Apply(const OUString &rStr) const
Definition: SwRewriter.cxx:43
void AddRule(SwUndoArg eWhat, const OUString &rWith)
Definition: SwRewriter.cxx:29
std::pair< SwUndoArg, OUString > SwRewriteRule
Definition: SwRewriter.hxx:34
SwUndoArg
Definition: SwRewriter.hxx:27