Freeciv21
Develop your civilization from humble roots to a global empire
astring.cpp
Go to the documentation of this file.
1 /*__ ___ ***************************************
2 / \ / \ Copyright (c) 1996-2020 Freeciv21 and Freeciv
3 \_ \ / __/ contributors. This file is part of Freeciv21.
4  _\ \ / /__ Freeciv21 is free software: you can redistribute it
5  \___ \____/ __/ and/or modify it under the terms of the GNU General
6  \_ _/ Public License as published by the Free Software
7  | @ @ \_ Foundation, either version 3 of the License,
8  | or (at your option) any later version.
9  _/ /\ You should have received a copy of the GNU
10  /o) (o/\ \_ General Public License along with Freeciv21.
11  \_____/ / If not, see https://www.gnu.org/licenses/.
12  \____/ ********************************************************/
13 
14 #include "astring.h"
15 
16 #include "fcintl.h"
17 
18 #include <QStringList>
19 
21 {
22  if (psv.size() == 1) {
23  // TRANS: "or"-separated string list with one single item.
24  return QString(Q_("?or-list-single:%1")).arg(psv[0]);
25  } else if (psv.size() == 2) {
26  // TRANS: "or"-separated string list with 2 items.
27  return QString(Q_("?or-list:%1 or %2")).arg(psv[0], psv[1]);
28  } else {
29  /* TRANS: start of an "or"-separated string list with more than two
30  * items. */
31  auto result = QString(Q_("?or-list:%1")).arg(psv[0]);
32  for (int i = 1; i < psv.size() - 1; ++i) {
33  /* TRANS: next elements of an "or"-separated string list with more
34  * than two items. */
35  result += QString(Q_("?or-list:, %1")).arg(psv[i]);
36  }
37  /* TRANS: end of an "or"-separated string list with more than two
38  * items. */
39  return result + QString(Q_("?or-list:, or %1")).arg(psv.back());
40  }
41 }
42 
44 {
45  if (psv.size() == 1) {
46  // TRANS: "and"-separated string list with one single item.
47  return QString(Q_("?and-list-single:%1")).arg(psv[0]);
48  } else if (psv.size() == 2) {
49  // TRANS: "and"-separated string list with 2 items.
50  return QString(Q_("?and-list:%1 and %2")).arg(psv[0], psv[1]);
51  } else {
52  // TRANS: start of an "and"-separated string list with more than two
53  // items.
54  auto result = QString(Q_("?and-list:%1")).arg(psv[0]);
55  for (int i = 1; i < psv.size() - 1; ++i) {
56  // TRANS: next elements of an "and"-separated string list with more
57  // than two items.
58  result += QString(Q_("?and-list:, %1")).arg(psv[i]);
59  }
60  // TRANS: end of an "and"-separated string list with more than two items.
61  return result + QString(Q_("?and-list:, and %1")).arg(psv.back());
62  }
63 }
64 
65 QString qendl() { return QStringLiteral("\n"); }
66 
67 // break line after after n-th char
68 QString break_lines(const QString &src, int after)
69 {
70  QStringList broken = src.split(QStringLiteral(" "), Qt::SkipEmptyParts);
71  QString dst;
72 
73  int clen = 0;
74  while (!broken.isEmpty()) {
75  QString s = broken.takeFirst();
76  dst += s + " ";
77  clen += s.length();
78  if (s.contains('\n')) {
79  clen = 0;
80  continue;
81  }
82  if (clen > after) {
83  dst += qendl();
84  clen = 0;
85  }
86  }
87  return dst;
88 }
QString strvec_to_and_list(const QVector< QString > &psv)
Definition: astring.cpp:43
QString break_lines(const QString &src, int after)
Definition: astring.cpp:68
QString strvec_to_or_list(const QVector< QString > &psv)
Definition: astring.cpp:20
QString qendl()
Definition: astring.cpp:65
#define Q_(String)
Definition: fcintl.h:53