Line data Source code
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 :
25 543 : bool operator == (const SwRewriteRule & a, const SwRewriteRule & b)
26 : {
27 543 : return a.first == b.first;
28 : }
29 :
30 32869 : SwRewriter::SwRewriter()
31 : {
32 32869 : }
33 :
34 0 : SwRewriter::SwRewriter(const SwRewriter & rSrc)
35 0 : : mRules(rSrc.mRules)
36 : {
37 0 : }
38 :
39 32869 : SwRewriter::~SwRewriter()
40 : {
41 32869 : }
42 :
43 22805 : void SwRewriter::AddRule(SwUndoArg eWhat, const OUString & rWith)
44 : {
45 22805 : SwRewriteRule aRule(eWhat, rWith);
46 :
47 22805 : vector<SwRewriteRule>::iterator aIt;
48 :
49 22805 : aIt = find(mRules.begin(), mRules.end(), aRule);
50 :
51 22805 : if (aIt != mRules.end())
52 0 : *aIt = aRule;
53 : else
54 22805 : mRules.push_back(aRule);
55 22805 : }
56 :
57 32869 : OUString SwRewriter::Apply(const OUString & rStr) const
58 : {
59 32869 : OUString aResult = rStr;
60 :
61 55675 : for (std::vector<SwRewriteRule>::const_iterator aIt = mRules.begin(); aIt != mRules.end(); ++aIt)
62 : {
63 22806 : aResult = aResult.replaceAll(GetPlaceHolder(aIt->first), aIt->second);
64 : }
65 :
66 32869 : return aResult;
67 : }
68 :
69 24844 : OUString SwRewriter::GetPlaceHolder(SwUndoArg eId)
70 : {
71 24844 : switch (eId)
72 : {
73 : case UndoArg1:
74 22238 : return OUString("$1");
75 : case UndoArg2:
76 2425 : return OUString("$2");
77 : case UndoArg3:
78 181 : return OUString("$3");
79 : default:
80 0 : break;
81 : }
82 0 : return OUString("$1");
83 : }
84 :
85 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|