Segmenter.h 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /*
  2. * Copyright (c) 2022, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #pragma once
  8. #include <AK/String.h>
  9. #include <LibJS/Runtime/Object.h>
  10. namespace JS::Intl {
  11. class Segmenter final : public Object {
  12. JS_OBJECT(Segmenter, Object);
  13. JS_DECLARE_ALLOCATOR(Segmenter);
  14. public:
  15. enum class SegmenterGranularity {
  16. Grapheme,
  17. Word,
  18. Sentence,
  19. };
  20. virtual ~Segmenter() override = default;
  21. String const& locale() const { return m_locale; }
  22. void set_locale(String locale) { m_locale = move(locale); }
  23. SegmenterGranularity segmenter_granularity() const { return m_segmenter_granularity; }
  24. void set_segmenter_granularity(StringView);
  25. StringView segmenter_granularity_string() const;
  26. private:
  27. explicit Segmenter(Object& prototype);
  28. String m_locale; // [[Locale]]
  29. SegmenterGranularity m_segmenter_granularity { SegmenterGranularity::Grapheme }; // [[SegmenterGranularity]]
  30. };
  31. ThrowCompletionOr<NonnullGCPtr<Object>> create_segment_data_object(VM&, Segmenter const&, Utf16View const&, double start_index, double end_index);
  32. enum class Direction {
  33. Before,
  34. After,
  35. };
  36. double find_boundary(Segmenter const&, Utf16View const&, double start_index, Direction);
  37. }