Welcome to mirror list, hosted at ThFree Co, Russian Federation.

editable_map_object.cpp « indexer - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 24bd07b50f49978b9a2bed9551e8a7be17fe6bff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
#include "indexer/editable_map_object.hpp"

#include "indexer/classificator.hpp"
#include "indexer/cuisines.hpp"
#include "indexer/postcodes_matcher.hpp"

#include "platform/preferred_languages.hpp"

#include "base/control_flow.hpp"
#include "base/macros.hpp"
#include "base/string_utils.hpp"

#include <algorithm>
#include <cctype>
#include <cmath>
#include <codecvt>
#include <regex>
#include <sstream>

using namespace std;

namespace
{
size_t const kFakeNamesCount = 2;

bool ExtractName(StringUtf8Multilang const & names, int8_t const langCode,
                 vector<osm::LocalizedName> & result)
{
  if (StringUtf8Multilang::kUnsupportedLanguageCode == langCode ||
      StringUtf8Multilang::kDefaultCode == langCode)
  {
    return false;
  }

  // Exclude languages that are already present.
  auto const it =
      find_if(result.begin(), result.end(), [langCode](osm::LocalizedName const & localizedName) {
        return localizedName.m_code == langCode;
      });

  if (result.end() != it)
    return false;

  string name;
  names.GetString(langCode, name);
  result.emplace_back(langCode, name);

  return true;
}

size_t PushMwmLanguages(StringUtf8Multilang const & names, vector<int8_t> const & mwmLanguages,
                        vector<osm::LocalizedName> & result)
{
  size_t count = 0;
  static size_t const kMaxCountMwmLanguages = 2;

  for (size_t i = 0; i < mwmLanguages.size() && count < kMaxCountMwmLanguages; ++i)
  {
    if (ExtractName(names, mwmLanguages[i], result))
      ++count;
  }

  return count;
}

char const * const kWebsiteProtocols[] = {"http://", "https://"};
size_t const kWebsiteProtocolDefaultIndex = 0;

size_t GetProtocolNameLength(string const & website)
{
  for (auto const & protocol : kWebsiteProtocols)
  {
    if (strings::StartsWith(website, protocol))
      return strlen(protocol);
  }
  return 0;
}

bool IsProtocolSpecified(string const & website)
{
  return GetProtocolNameLength(website) > 0;
}

osm::FakeNames MakeFakeSource(StringUtf8Multilang const & source,
                              vector<int8_t> const & mwmLanguages, StringUtf8Multilang & fakeSource)
{
  string defaultName;
  // Fake names works for mono language (official) speaking countries-only.
  if (mwmLanguages.size() != 1 || !source.GetString(StringUtf8Multilang::kDefaultCode, defaultName))
  {
    return {};
  }

  osm::FakeNames fakeNames;
  // Mwm name has higher priority then English name.
  array<int8_t, kFakeNamesCount> fillCandidates = {{mwmLanguages.front(), StringUtf8Multilang::kEnglishCode}};
  fakeSource = source;

  string tempName;
  for (auto const code : fillCandidates)
  {
    if (!source.GetString(code, tempName))
    {
      tempName = defaultName;
      fakeSource.AddString(code, defaultName);
    }

    fakeNames.m_names.emplace_back(code, tempName);
  }

  fakeNames.m_defaultName = defaultName;

  return fakeNames;
}

// Tries to set default name from the localized name. Returns false when there's no such localized name.
bool TryToFillDefaultNameFromCode(int8_t const code, StringUtf8Multilang & names)
{
  string newDefaultName;
  if (code != StringUtf8Multilang::kUnsupportedLanguageCode)
    names.GetString(code, newDefaultName);

  // Default name can not be empty.
  if (!newDefaultName.empty())
  {
    names.AddString(StringUtf8Multilang::kDefaultCode, newDefaultName);
    return true;
  }

  return false;
}

// Tries to set default name to any non-empty localized name.
// This is the case when fake names were cleared.
void TryToFillDefaultNameFromAnyLanguage(StringUtf8Multilang & names)
{
  names.ForEach([&names](int8_t langCode, string const & name)
  {
    if (name.empty() || langCode == StringUtf8Multilang::kDefaultCode)
      return base::ControlFlow::Continue;

    names.AddString(StringUtf8Multilang::kDefaultCode, name);
    return base::ControlFlow::Break;
  });
}

void RemoveFakesFromName(osm::FakeNames const & fakeNames, StringUtf8Multilang & name)
{
  vector<int8_t> codesToExclude;
  string defaultName;
  name.GetString(StringUtf8Multilang::kDefaultCode, defaultName);

  for (auto const & item : fakeNames.m_names)
  {
    string tempName;
    if (!name.GetString(item.m_code, tempName))
      continue;
    // No need to save in case when name is empty, duplicate of default name or was not changed.
    if (tempName.empty() || tempName == defaultName ||
        (tempName == item.m_filledName && tempName == fakeNames.m_defaultName))
    {
      codesToExclude.push_back(item.m_code);
    }
  }

  if (codesToExclude.empty())
    return;

  StringUtf8Multilang nameWithoutFakes;
  name.ForEach([&codesToExclude, &nameWithoutFakes](int8_t langCode, string const & value)
  {
    auto const it = find(codesToExclude.begin(), codesToExclude.end(), langCode);
    if (it == codesToExclude.end())
      nameWithoutFakes.AddString(langCode, value);
  });

  name = nameWithoutFakes;
}
}  // namespace

namespace osm
{
// LocalizedName -----------------------------------------------------------------------------------

LocalizedName::LocalizedName(int8_t const code, string const & name)
  : m_code(code)
  , m_lang(StringUtf8Multilang::GetLangByCode(code))
  , m_langName(StringUtf8Multilang::GetLangNameByCode(code))
  , m_name(name)
{
}

LocalizedName::LocalizedName(string const & langCode, string const & name)
  : m_code(StringUtf8Multilang::GetLangIndex(langCode))
  , m_lang(StringUtf8Multilang::GetLangByCode(m_code))
  , m_langName(StringUtf8Multilang::GetLangNameByCode(m_code))
  , m_name(name)
{
}

// EditableMapObject -------------------------------------------------------------------------------

// static
int8_t const EditableMapObject::kMaximumLevelsEditableByUsers = 25;

bool EditableMapObject::IsNameEditable() const { return m_editableProperties.m_name; }
bool EditableMapObject::IsAddressEditable() const { return m_editableProperties.m_address; }

vector<Props> EditableMapObject::GetEditableProperties() const
{
  auto props = MetadataToProps(m_editableProperties.m_metadata);
  if (m_editableProperties.m_cuisine)
  {
    props.push_back(Props::Cuisine);
    base::SortUnique(props);
  }

  return props;
}

vector<feature::Metadata::EType> const & EditableMapObject::GetEditableFields() const
{
  return m_editableProperties.m_metadata;
}

NamesDataSource EditableMapObject::GetNamesDataSource(bool needFakes /* = true */)
{
  auto const mwmInfo = GetID().m_mwmId.GetInfo();

  if (!mwmInfo)
    return NamesDataSource();

  vector<int8_t> mwmLanguages;
  mwmInfo->GetRegionData().GetLanguages(mwmLanguages);

  auto const userLangCode = StringUtf8Multilang::GetLangIndex(languages::GetCurrentNorm());

  if (needFakes)
  {
    StringUtf8Multilang fakeSource;
    m_fakeNames = MakeFakeSource(m_name, mwmLanguages, fakeSource);

    if (!m_fakeNames.m_names.empty())
      return GetNamesDataSource(fakeSource, mwmLanguages, userLangCode);
  }
  else
  {
    RemoveFakeNames(m_fakeNames, m_name);
  }

  return GetNamesDataSource(m_name, mwmLanguages, userLangCode);
}

// static
NamesDataSource EditableMapObject::GetNamesDataSource(StringUtf8Multilang const & source,
                                                      vector<int8_t> const & mwmLanguages,
                                                      int8_t const userLangCode)
{
  NamesDataSource result;
  auto & names = result.names;
  auto & mandatoryCount = result.mandatoryNamesCount;
  // Push Mwm languages.
  mandatoryCount = PushMwmLanguages(source, mwmLanguages, names);

  // Push english name.
  if (ExtractName(source, StringUtf8Multilang::kEnglishCode, names))
    ++mandatoryCount;

  // Push user's language.
  if (ExtractName(source, userLangCode, names))
    ++mandatoryCount;

  // Push other languages.
  source.ForEach([&names, mandatoryCount](int8_t const code, string const & name) {
    // Exclude default name.
    if (StringUtf8Multilang::kDefaultCode == code)
      return;

    auto const mandatoryNamesEnd = names.begin() + mandatoryCount;
    // Exclude languages which are already in container (languages with top priority).
    auto const it = find_if(
        names.begin(), mandatoryNamesEnd,
        [code](LocalizedName const & localizedName) { return localizedName.m_code == code; });

    if (mandatoryNamesEnd == it)
      names.emplace_back(code, name);
  });

  return result;
}

vector<LocalizedStreet> const & EditableMapObject::GetNearbyStreets() const { return m_nearbyStreets; }

string EditableMapObject::GetPostcode() const
{
  return m_metadata.Get(feature::Metadata::FMD_POSTCODE);
}

string EditableMapObject::GetWikipedia() const
{
  return m_metadata.Get(feature::Metadata::FMD_WIKIPEDIA);
}

void EditableMapObject::ForEachMetadataItem(
    bool skipSponsored, function<void(string const & tag, string const & value)> const & fn) const
{
  for (auto const type : m_metadata.GetKeys())
  {
    if (skipSponsored && m_metadata.IsSponsoredType(type))
      continue;
    auto const attributeName = ToString(type);
    fn(attributeName, m_metadata.Get(type));
  }
}

uint64_t EditableMapObject::GetTestId() const
{
  istringstream iss(m_metadata.Get(feature::Metadata::FMD_TEST_ID));
  uint64_t id;
  iss >> id;
  return id;
}

void EditableMapObject::SetTestId(uint64_t id)
{
  ostringstream oss;
  oss << id;
  m_metadata.Set(feature::Metadata::FMD_TEST_ID, oss.str());
}

void EditableMapObject::SetEditableProperties(osm::EditableProperties const & props)
{
  m_editableProperties = props;
}

void EditableMapObject::SetName(StringUtf8Multilang const & name) { m_name = name; }

void EditableMapObject::SetName(string name, int8_t langCode)
{
  strings::Trim(name);

  if (m_namesAdvancedMode)
  {
    m_name.AddString(langCode, name);
    return;
  }

  if (!name.empty() && !m_name.HasString(StringUtf8Multilang::kDefaultCode))
  {
    const auto mwmInfo = GetID().m_mwmId.GetInfo();

    if (mwmInfo)
    {
      vector<int8_t> mwmLanguages;
      mwmInfo->GetRegionData().GetLanguages(mwmLanguages);

      if (CanUseAsDefaultName(langCode, mwmLanguages))
      {
        m_name.AddString(StringUtf8Multilang::kDefaultCode, name);
        return;
      }
    }
  }

  m_name.AddString(langCode, name);
}

// static
bool EditableMapObject::CanUseAsDefaultName(int8_t const lang, vector<int8_t> const & mwmLanguages)
{
  for (auto const & mwmLang : mwmLanguages)
  {
    if (StringUtf8Multilang::kUnsupportedLanguageCode == mwmLang)
      continue;

    if (lang == mwmLang)
      return true;
  }

  return false;
}

// static
void EditableMapObject::RemoveFakeNames(FakeNames const & fakeNames, StringUtf8Multilang & name)
{
  if (fakeNames.m_names.empty())
    return;

  int8_t newDefaultNameCode = StringUtf8Multilang::kUnsupportedLanguageCode;
  size_t changedCount = 0;
  string defaultName;
  name.GetString(StringUtf8Multilang::kDefaultCode, defaultName);

  // New default name calculation priority: 1. name on mwm language, 2. english name.
  for (auto it = fakeNames.m_names.rbegin(); it != fakeNames.m_names.rend(); ++it)
  {
    string tempName;
    if (!name.GetString(it->m_code, tempName))
      continue;

    if (tempName != it->m_filledName)
    {
      if (!tempName.empty())
        newDefaultNameCode = it->m_code;

      ++changedCount;
    }
  }

  // If all previously filled fake names were changed - try to change the default name.
  if (changedCount == fakeNames.m_names.size())
  {
    if (!TryToFillDefaultNameFromCode(newDefaultNameCode, name))
      TryToFillDefaultNameFromAnyLanguage(name);
  }

  RemoveFakesFromName(fakeNames, name);
}

void EditableMapObject::SetMercator(m2::PointD const & center) { m_mercator = center; }

void EditableMapObject::SetType(uint32_t featureType)
{
  if (m_types.GetGeomType() == feature::GeomType::Undefined)
  {
    // Support only point type for newly created features.
    m_types = feature::TypesHolder(feature::GeomType::Point);
    m_types.Assign(featureType);
  }
  else
  {
    // Correctly replace "main" type in cases when feature holds more types.
    ASSERT(!m_types.Empty(), ());
    feature::TypesHolder copy = m_types;
    // TODO(mgsergio): Replace by correct sorting from editor's config.
    copy.SortBySpec();
    m_types.Remove(*copy.begin());
    m_types.Add(featureType);
  }
}

void EditableMapObject::SetTypes(feature::TypesHolder const & types) { m_types = types; }

void EditableMapObject::SetID(FeatureID const & fid) { m_featureID = fid; }
void EditableMapObject::SetStreet(LocalizedStreet const & st) { m_street = st; }

void EditableMapObject::SetNearbyStreets(vector<LocalizedStreet> && streets)
{
  m_nearbyStreets = move(streets);
}

void EditableMapObject::SetHouseNumber(string const & houseNumber)
{
  m_houseNumber = houseNumber;
}

bool EditableMapObject::UpdateMetadataValue(string const & key, string const & value)
{
  feature::Metadata::EType mdType;
  if (!feature::Metadata::TypeFromString(key, mdType))
    return false;
  m_metadata.Set(mdType, value);
  return true;
}

void EditableMapObject::SetPostcode(string const & postcode)
{
  m_metadata.Set(feature::Metadata::FMD_POSTCODE, postcode);
}

void EditableMapObject::SetPhone(string const & phone)
{
  m_metadata.Set(feature::Metadata::FMD_PHONE_NUMBER, phone);
}

void EditableMapObject::SetFax(string const & fax)
{
  m_metadata.Set(feature::Metadata::FMD_FAX_NUMBER, fax);
}

void EditableMapObject::SetEmail(string const & email)
{
  m_metadata.Set(feature::Metadata::FMD_EMAIL, email);
}

void EditableMapObject::SetWebsite(string website)
{
  if (!website.empty() && !IsProtocolSpecified(website))
    website = kWebsiteProtocols[kWebsiteProtocolDefaultIndex] + website;

  m_metadata.Set(feature::Metadata::FMD_WEBSITE, website);
  m_metadata.Drop(feature::Metadata::FMD_URL);
}

void EditableMapObject::SetInternet(Internet internet)
{
  m_metadata.Set(feature::Metadata::FMD_INTERNET, DebugPrint(internet));

  static auto const wifiType = classif().GetTypeByPath({"internet_access", "wlan"});

  if (m_types.Has(wifiType) && internet != Internet::Wlan)
    m_types.Remove(wifiType);
  else if (!m_types.Has(wifiType) && internet == Internet::Wlan)
    m_types.Add(wifiType);
}

void EditableMapObject::SetStars(int stars)
{
  if (stars > 0 && stars <= 7)
    m_metadata.Set(feature::Metadata::FMD_STARS, strings::to_string(stars));
  else
    LOG(LWARNING, ("Ignored invalid value to Stars:", stars));
}

void EditableMapObject::SetOperator(string const & op)
{
  m_metadata.Set(feature::Metadata::FMD_OPERATOR, op);
}

void EditableMapObject::SetElevation(double ele)
{
  // TODO: Reuse existing validadors in generator (osm2meta).
  constexpr double kMaxElevationOnTheEarthInMeters = 10000;
  constexpr double kMinElevationOnTheEarthInMeters = -15000;
  if (ele < kMaxElevationOnTheEarthInMeters && ele > kMinElevationOnTheEarthInMeters)
    m_metadata.Set(feature::Metadata::FMD_ELE, strings::to_string_dac(ele, 1));
  else
    LOG(LWARNING, ("Ignored invalid value to Elevation:", ele));
}

void EditableMapObject::SetWikipedia(string const & wikipedia)
{
  m_metadata.Set(feature::Metadata::FMD_WIKIPEDIA, wikipedia);
}

void EditableMapObject::SetFlats(string const & flats)
{
  m_metadata.Set(feature::Metadata::FMD_FLATS, flats);
}

void EditableMapObject::SetBuildingLevels(string const & buildingLevels)
{
  m_metadata.Set(feature::Metadata::FMD_BUILDING_LEVELS, buildingLevels);
}

void EditableMapObject::SetLevel(string const & level)
{
  m_metadata.Set(feature::Metadata::FMD_LEVEL, level);
}

LocalizedStreet const & EditableMapObject::GetStreet() const { return m_street; }

void EditableMapObject::SetCuisines(vector<string> const & cuisines)
{
  FeatureParams params;
  params.m_types.insert(params.m_types.begin(), m_types.begin(), m_types.end());
  for (auto const & cuisine : cuisines)
    params.m_types.push_back(classif().GetTypeByPath({"cuisine", cuisine}));

  // Move useless types to the end and resize to fit TypesHolder.
  params.FinishAddingTypes();

  feature::TypesHolder types;
  for (auto const t : params.m_types)
    types.Add(t);

  m_types = types;
}

void EditableMapObject::SetOpeningHours(string const & openingHours)
{
  m_metadata.Set(feature::Metadata::FMD_OPEN_HOURS, openingHours);
}

void EditableMapObject::SetPointType() { m_geomType = feature::GeomType::Point; }

void EditableMapObject::RemoveBlankAndDuplicationsForDefault()
{
  StringUtf8Multilang editedName;
  string defaultName;
  m_name.GetString(StringUtf8Multilang::kDefaultCode, defaultName);

  m_name.ForEach([&defaultName, &editedName](int8_t langCode, string const & name)
  {
    auto const duplicate = langCode != StringUtf8Multilang::kDefaultCode && defaultName == name;
    if (!name.empty() && !duplicate)
      editedName.AddString(langCode, name);
  });

  m_name = editedName;
}

void EditableMapObject::RemoveNeedlessNames()
{
  if (!IsNamesAdvancedModeEnabled())
    RemoveFakeNames(m_fakeNames, m_name);

  RemoveBlankAndDuplicationsForDefault();
}

// static
bool EditableMapObject::ValidateBuildingLevels(string const & buildingLevels)
{
  if (buildingLevels.empty())
    return true;

  if (buildingLevels.size() > 18 /* max number of digits in uint_64 */)
    return false;

  if ('0' == buildingLevels.front())
    return false;

  uint64_t levels;
  return strings::to_uint64(buildingLevels, levels) && levels > 0 && levels <= kMaximumLevelsEditableByUsers;
}

// static
bool EditableMapObject::ValidateHouseNumber(string const & houseNumber)
{
  // TODO(mgsergio): Use LooksLikeHouseNumber!

  if (houseNumber.empty())
    return true;

  strings::UniString us = strings::MakeUniString(houseNumber);
  // TODO: Improve this basic limit - it was choosen by @Zverik.
  auto constexpr kMaxHouseNumberLength = 15;
  if (us.size() > kMaxHouseNumberLength)
    return false;

  // TODO: Should we allow arabic numbers like U+0661 ١	Arabic-Indic Digit One?
  strings::NormalizeDigits(us);
  for (auto const c : us)
  {
    // Valid house numbers contain at least one digit.
    if (strings::IsASCIIDigit(c))
      return true;
  }
  return false;
}

// static
bool EditableMapObject::ValidateFlats(string const & flats)
{
  for (auto it = strings::SimpleTokenizer(flats, ";"); it; ++it)
  {
    auto token = *it;
    strings::Trim(token);

    vector<string> range;
    for (auto i = strings::SimpleTokenizer(token, "-"); i; ++i)
      range.push_back(*i);
    if (range.empty() || range.size() > 2)
      return false;

    for (auto const & rangeBorder : range)
    {
      if (!all_of(begin(rangeBorder), end(rangeBorder), ::isalnum))
        return false;
    }
  }
  return true;
}

// static
bool EditableMapObject::ValidatePostCode(string const & postCode)
{
  if (postCode.empty())
    return true;
  return search::LooksLikePostcode(postCode, false /* IsPrefix */);
}

// static
bool EditableMapObject::ValidatePhoneList(string const & phone)
{
  // BNF:
  // <digit>            ::= '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9'
  // <available_char>   ::= ' ' | '+' | '-' | '(' | ')'
  // <delimeter>        ::= ',' | ';'
  // <phone>            ::= (<digit> | <available_chars>)+
  // <phone_list>       ::= '' | <phone> | <phone> <delimeter> <phone_list>

  if (phone.empty())
    return true;

  auto const kMaxNumberLen = 15;
  auto const kMinNumberLen = 5;

  if (phone.size() < kMinNumberLen)
    return false;

  auto curr = phone.begin();
  auto last = phone.begin();

  do
  {
    last = find_if(curr, phone.end(), [](string::value_type const & ch)
    {
      return ch == ',' || ch == ';';
    });

    auto digitsCount = 0;
    string const symbols = "+-() ";
    for (; curr != last; ++curr)
    {
      if (!isdigit(*curr) && find(symbols.begin(), symbols.end(), *curr) == symbols.end())
        return false;

      if (isdigit(*curr))
        ++digitsCount;
    }

    if (digitsCount < kMinNumberLen || digitsCount > kMaxNumberLen)
      return false;

    curr = last;
  }
  while (last != phone.end() && ++curr != phone.end());

  return true;
}

// static
bool EditableMapObject::ValidateWebsite(string const & site)
{
  if (site.empty())
    return true;

  auto const startPos = GetProtocolNameLength(site);

  if (startPos >= site.size())
    return false;

  // Site should contain at least one dot but not at the begining/end.
  if ('.' == site[startPos] || '.' == site.back())
    return false;

  if (string::npos == site.find("."))
    return false;

  if (string::npos != site.find(".."))
    return false;

  return true;
}

// static
bool EditableMapObject::ValidateEmail(string const & email)
{
  if (email.empty())
    return true;

  if (strings::IsASCIIString(email))
    return regex_match(email, regex(R"([^@\s]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)+$)"));

  if ('@' == email.front() || '@' == email.back())
    return false;

  if ('.' == email.back())
    return false;

  auto const atPos = find(begin(email), end(email), '@');
  if (atPos == end(email))
    return false;

  // There should be only one '@' sign.
  if (find(next(atPos), end(email), '@') != end(email))
    return false;

  // There should be at least one '.' sign after '@'
  if (find(next(atPos), end(email), '.') == end(email))
    return false;

  return true;
}

// static
bool EditableMapObject::ValidateLevel(string const & level)
{
  if (level.empty())
    return true;

  if (level.size() > 4 /* 10.5, for example */)
    return false;

  // Allowing only half-levels.
  if (level.find('.') != string::npos && !strings::EndsWith(level, ".5"))
    return false;

  // Forbid "04" and "0.".
  if ('0' == level.front() && level.size() == 2)
    return false;

  auto constexpr kMinBuildingLevel = -9;
  double result;
  return strings::to_double(level, result) && result > kMinBuildingLevel &&
         result <= kMaximumLevelsEditableByUsers;
}

// static
bool EditableMapObject::ValidateName(string const & name)
{
  if (name.empty())
    return true;

  if (strings::IsASCIIString(name))
    return regex_match(name, regex(R"(^[ A-Za-z0-9.,?!@#$%&()\-\+:;"'`]+$)"));

  std::wstring_convert<std::codecvt_utf8<char32_t>, char32_t> converter;

  std::u32string u32name;
  try
  {
    u32name = converter.from_bytes(name);
  }
  catch (std::range_error const &)
  {
    // Cannot convert, for ex. it is possible for some emoji.
    return false;
  }

  std::u32string const excludedSymbols = U"^~§><{}[]*=_±\n\t\r\v\f|√•÷׶°";

  for (auto const ch : u32name)
  {
    // Exclude arrows, mathematical symbols, borders, geometric shapes.
    if (ch >= U'\U00002190' && ch <= U'\U00002BFF')
      return false;
    // Exclude format controls, musical symbols, emoticons, ornamental and pictographs,
    // ancient and exotic alphabets.
    if (ch >= U'\U0000FFF0' && ch <= U'\U0001F9FF')
      return false;

    if (find(excludedSymbols.cbegin(), excludedSymbols.cend(), ch) != excludedSymbols.cend())
      return false;
  }
  return true;
}
}  // namespace osm